FizzBuzzWhizz

经分析该问题主要需要解决一下问题:

1.判断公倍数。
2.实现字符串连接
解决方法如下:
1.依次判断是否是3、5、7的倍数,如果是则将相应字符串进行连接。
2.连接字符串的方式,以名值对的方式将3、5、7和相应字符串进行关联,当需要进行连接的时候只需将3、5、7对应字符串加入结果字符串即可。

/* 
FizzBuzzWhizz 需求:

when run(3) return 'Fizz'
when run(5) return 'Buzz'
when run(7) return 'Whizz'
when run(3 * 5) return 'FizzBuzz'   //run 的参数为 3和5共同的倍数
when run(3 * 7) return 'FizzWhizz'   //run 的参数为 3和7共同的倍数
when run(5 * 7) return 'BuzzWhizz'   //run 的参数为 5和7共同的倍数
when run(3 * 5 * 7) return 'FizzBuzzWhizz'   //run 的参数为 3、5和7共同的倍数

其余情况:
when run(n) return n   // n 可以为 非 3、5、 7或3、5、7的倍数的数字 如:run(2) return '2'

作业要求:
1.通过document.write()方法,将以上需求的每一条调用结果打印出来,结果会在屏幕右边显示
2.为每一条需求写一个测试,测试例子请见实例方法 test1

*/


function run(num) {
    var result=''
    var strtest={
  '3':'Fizz',
  '5':'Buzz',
  '7':'Whizz'
  }
  if(num%3==0){
  result+=strtest['3']
  }
  if(num%5==0){
  result+=strtest['5']
  }
  if(num%7==0){
  result+=strtest['7']
  }
  if(result==''){
  result+=num.toString()
  }
  return result
}

var test1 = function() {
    let result = run(3);    
  if(result != 'Fizz') {
    document.write('The test 1 failed')
  }else {
    document.write('The test 1 result is : '+ result+'
') } } test1() var test2 = function() { let result = run(5); if(result != 'Buzz') { document.write('The test 2 failed') }else { document.write('The test 2 result is : '+ result+'
') } } test2() var test3 = function() { let result = run(7); if(result != 'Whizz') { document.write('The test 3 failed') }else { document.write('The test 3 result is : '+ result+'
') } } test3() var test4 = function() { let result = run(3*5); if(result != 'FizzBuzz') { document.write('The test 4 failed') }else { document.write('The test 4 result is : '+ result+'
') } } test4() var test5 = function() { let result = run(3*7); if(result != 'FizzWhizz') { document.write('The test 5 failed') }else { document.write('The test 5 result is : '+ result+'
') } } test5() var test6 = function() { let result = run(5*7); if(result != 'BuzzWhizz') { document.write('The test 6 failed') }else { document.write('The test 6 result is : '+ result+'
') } } test6() var test7 = function() { let result = run(3*5*7); if(result != 'FizzBuzzWhizz') { document.write('The test 7 failed') }else { document.write('The test 7 result is : '+ result+'
') } } 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+'
') } } 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 result is : 2

你可能感兴趣的:(FizzBuzzWhizz)