function run(num) {
let x="Fizz",y="Buzz",z="Whizz",l="FizzBuzz",m="FizzWhizz",n="BuzzWhizz",q="FizzBuzzWhizz";
if(num%105==0)
return q;
else if(num%35==0)
return n;
else if(num%21==0)
return m;
else if(num%15==0)
return l;
else if(num==7)
return z;
else if(num==5)
return y;
else if(num==3)
return x;
else
return num.toString();
}
var test1 = function() {
let result = run(3);
if(result == 3) {
document.write('The test 1 failed')
}else {
document.write('The test 1 result is : '+ result)
}
document.write("
")
}
test1()
var test2 = function() {
let result = run(5);
if(result == 5) {
document.write('The test 2 failed')
}else {
document.write('The test 2 result is : '+ result)
}
document.write("
")
}
test2()
var test3 = function() {
let result = run(7);
if(result == 7) {
document.write('The test 3 failed')
}else {
document.write('The test 3 result is : '+ result)
}
document.write("
")
}
test3()
var test4 = function() {
let result = run(15);
if(result == 15) {
document.write('The test 4 failed')
}else {
document.write('The test 4 result is : '+ result)
}
document.write("
")
}
test4()
var test5 = function() {
let result = run(21);
if(result == 21) {
document.write('The test 5 failed')
}else {
document.write('The test 5 result is : '+ result)
}
document.write("
")
}
test5()
var test6 = function() {
let result = run(35);
if(result == 35) {
document.write('The test 6 failed')
}else {
document.write('The test 6 result is : '+ result)
}
document.write("
")
}
test6()
var test7 = function() {
let result = run(105);
if(result == 105) {
document.write('The test 7 failed')
}else {
document.write('The test 7 result is : '+ result)
}
document.write("
")
}
test7()
var test8 = function() {
let result = run(2);
if(result == 2) {
document.write('The test 8 failed')
}else {
document.write('The test 8 result is : '+ result)
}
document.write("
")
}
test8()
测试结果如下
The test 1 result is : Fizz
The test 2 result is : Buzz
The test 3 result is : Whizz
The test 4 result is : FizzBuzz
The test 5 result is : FizzWhizz
The test 6 result is : BuzzWhizz
The test 7 result is : FizzBuzzWhizz
The test 8 failed
收获:学会了这个在线编译器的使用,实现了测试,在此过程应该注意if语句判断应该从大到小的范围。