作业要求:
1.通过document.write()方法,将以上需求的每一条调用结果打印出来,结果会在屏幕右边显示
2.为每一条需求写一个测试
代码:
function run(num) {
if(num === 3){
return 'Fizz';
}
if(num === 5){
return 'Buzz';
}
if(num === 7){
return 'Whizz';
}
if(num%3===0 && num%5===0 && num%7===0){
return 'FizzBuzzWhizz';
}
if(num%3===0 && num%5===0){
return 'FizzBuzz';
}
if(num%3===0 && num%7===0){return 'FizzWhizz';
}
if(num%5===0 && num%7===0){
return 'BuzzWhizz';
}
return num.toString();
}
var test1 = function() {
let result = run(2);
if(result != 2) {
document.write('The test 1 failed')
}else {
document.write('The test 1 result is : '+ result)
}
}
test1()
var test2 = function() {
let result = run(3);
if(result != 'Fizz') {
document.write('The test 2 failed')
}else {
document.write('The test 2 result is : '+ result)
}
}
test2()
var test3 = function() {
let result = run(5);
if(result != 'Buzz') {
document.write('The test 3 failed')
}else {
document.write('The test 3 result is : '+ result)
}
}
test3()
var test4 = function() {
let result = run(7);
if(result != 'Whizz') {
document.write('The test 4 failed')
}else {
document.write('The test 4 result is : '+ result)
}
}
test4()
var test5 = function() {
let result = run(3 * 5);
if(result != 'FizzBuzz') {
document.write('The test 5 failed')
}else { document.write('The test 5 result is : '+ result)
}
}
test5()
var test6 = function() {
let result = run(3 * 7);
if(result != 'FizzWhizz') {
document.write('The test 6 failed')
}else {
document.write('The test 6 result is : '+ result)
}
}
test6()
var test7 = function() {
let result = run(5 * 7);
if(result != 'BuzzWhizz') {
document.write('The test 7 failed')
}else {
document.write('The test 7 result is : '+ result)
}
}
test7()
var test8 = function() {
let result = run(3 * 5 * 7);
if(result !='FizzBuzzWhizz') {
document.write('The test 8 failed')
}else {
document.write('The test 8 result is : '+ result)
}
}
test8()
测试结果:The test 1 result is : 2
The test 2 result is : Fizz
The test 3 result is : Buzz
The test 4 result is : Whizz
The test 5 result is : FizzBuzz
The test 6 result is : FizzWhizz
The test 7 result is : BuzzWhizz
The test 8 result is : FizzBuzzWhizz
总结:遇到大的问题通过想清楚所有不同情况的分析运用多个判断语句解决大的问题。