function run(num) {
if(num==3)??
return 'Fizz';? ??
else if(num==5)? ?
?return 'Buzz';? ??
else if(num==7)? ??
?return 'Whizz';? ??
else if(num%3==0&&num%5==0)? ? {? ??
if(num%7==0)? ? ??
?return 'FizzBuzzWhizz';? ? ? ? ? ??
else? ? ? ? ? ??
return 'FizzBuzz'? ? ? }? ?
?else if(num%3==0&&num%7==0)? ?
?return 'FizzWhizz';? ??
else if(num%7==0&&num%5==0)? ??
?return 'BuzzWhizz';? ?
?else? ??
?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(15);?
?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(21);?
?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(35);?
?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(105);?
?if(result !='FizzBuzzWhizz')? {? document.write('The test 8 failed'+'')? }
else{? document.write('The test 8 result is : '+ result+'')? }}
test8()
個人總結(jié)
初次接觸JS的代碼和語法膏秫,發(fā)現(xiàn)與其他代碼的語法有相似之處泥兰,根據(jù)題目需求码俩,傳入一個數(shù),進行判斷蟀拷,返回一個符合要求的字符串。
第①個需求:當給run函數(shù)傳入數(shù)值3時,返回值應為Fizz寿弱。run(3),系統(tǒng)判斷傳入值為3,那么就return Fizz,把Fizz賦值給result變量按灶,通過document函數(shù)打印出來症革,需求實現(xiàn)。
做第②個需求:傳入5鸯旁,返回Buzz噪矛。run(5),判定傳入值為5,返回Fizz铺罢,賦給result艇挨,通過函數(shù)打印出來,第二個需求實現(xiàn)韭赘。
剩下的類似缩滨,判定是某個數(shù)值的倍數(shù),則傳入的數(shù)值對那個數(shù)進行取余,若余數(shù)為0脉漏,則表示能整除苞冯,符合判斷,返回相應的字符串侧巨,打印舅锄。