function run(num) {
if(num%3==0)
{
if(num%5==0)
{
if(num%7==0)
{
num='FizzBuzzWhizz';
}
else
{
num='FizzBuzz';
}
}
else if(num%7==0)
{
num='FizzWhizz';
}
else num='Fizz';
}
else if(num%5==0)
{
if(num%7==0)
{
num='BuzzWhizz';
}
else
{
num='Buzz';
}
}
else if(num%7==0)
{
num='Whizz';
}
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 + '
')
}
}
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 + '
')
}
}
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 + '
')
}
}
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 + '
')
}
}
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 + '
')
}
}
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 + '
')
}
}
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 + '
')
}
}
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 + '
')
}
}
test1()
test2()
test3()
test4()
test5()
test6()
test7()
test8()
这次作业算法是很简单的,没有遇到什么困难。但是我纠结于是把test函数通过传参的方式来测试还是每一个情况都写一个test,因为前者更抽象,能给不同情况使用。但是我最后还是选择了最接近题目要求的方式,每一个都写了一个测试。