FizzBuzzWhizz

作者: TW_1024_赵梓君 | 来源:发表于2017-03-29 14:42 被阅读47次

JavaScript

针对那道题,我是这么想的:

先判断是否为3 and 5 and 7的公倍数,若不是接着判断,为3 and 5、3 and 7、5 and 7的公倍数,若不是则接着判断是否为3、5、7的倍数······

代码:


function run(num){

if(num%3==0 && num%==0 && num%7==0)

return 'FizzBuzzWhizz';

else if(num%3==0 && num%5==0)

return 'FizzBuzz';

else if(num%5==0 && num%7==0)

return 'BuzzWhizz';

else if(num%3==0 && num%7==0)

return 'FizzWhizz';

else if(num%3==0)

return 'Fizz';

else if(num%5==0)

return 'Buzz';

else if(num%7==0)

return 'Whizz';

else

return num.toString();

}

测试Test:


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 test8 result is : '+ result)}}

test8()

相关文章

  • FizzBuzzWhizz

    本篇仅讨论此题目,若需查看与 javascript 相关信息,请查看本人另一篇文章 javascript 基础入门...

  • FizzBuzzWhizz

    题目描述:FizzBuzzWhizz 需求: when run(3) return 'Fizz'when run(...

  • FizzBuzzWhizz

    经分析该问题主要需要解决一下问题: 1.判断公倍数。2.实现字符串连接解决方法如下:1.依次判断是否是3、5、7的...

  • FizzBuzzWhizz

    需求分析如下: 1.run 的参数为 3、5和7共同的倍数,run 的参数为 5和7共同的倍数...可以转换为nu...

  • FizzBuzzWhizz

    JavaScript 针对那道题,我是这么想的: 先判断是否为3 and 5 and 7的公倍数,若不是接着判断,...

  • FizzBuzzWhizz

    FizzBuzzWhizz 起初考虑的时候觉得rule比较简单,运用最基本的思想,逐个判断条件发现确实太繁杂。做完...

  • FizzBuzzWhizz

    需求: when run(3) return 'Fizz'when run(5) return 'Buzz'whe...

  • FizzBuzzWhizz

    作业要求: 1.通过document.write()方法,将以上需求的每一条调用结果打印出来,结果会在屏幕右边显示...

  • FizzBuzzWhizz

    FizzBuzzWhizz题目需求: when run (3) return 'Fizz'when run(5) ...

  • FizzBuzzWhizz

    黑珍珠号_陈祥梅 题目需求 when run(3) return 'Fizz'when run(5) return...

网友评论

    本文标题:FizzBuzzWhizz

    本文链接:https://www.haomeiwen.com/subject/rsveottx.html