列表

详情


FED55. 流程控制

描述

实现 fizzBuzz 函数,参数 num 与返回值的关系如下:
1、如果 num 能同时被 3 和 5 整除,返回字符串 fizzbuzz
2、如果 num 能被 3 整除,返回字符串 fizz
3、如果 num 能被 5 整除,返回字符串 buzz
4、如果参数为空或者不是 Number 类型,返回 false
5、其余情况,返回参数 num

示例1

输入:

15

输出:

fizzbuzz

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 876ms, 内存消耗: 77876KB, 提交时间: 2020-12-28

{"css":"","js":"function fizzBuzz(num) {\nif (num % 3 === 0 && num % 5 === 0){\n    return 'fizzbuzz'\n} else if (num % 3 === 0){\n    return 'fizz'\n} else if (num % 5 === 0){\n    return 'buzz'\n} else if (num === null || typeof(num) !== 'number'){\n    return false\n} else {\n    return num\n}\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 877ms, 内存消耗: 77864KB, 提交时间: 2020-12-12

{"css":"","js":"function fizzBuzz(num) {\nif (num % 3 === 0 && num % 5 === 0){\n    return 'fizzbuzz'\n} else if (num % 3 === 0 && num % 5 !== 0){\n    return 'fizz'\n} else if (num % 5 === 0 && num % 3 !== 0 ){\n    return 'buzz'\n} else if (num === null || typeof(num) !== 'number'){\n    return false \n} else {\n    return num \n}\n}\nfizzBuzz(15);","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 877ms, 内存消耗: 77868KB, 提交时间: 2020-12-25

{"css":"","js":"function fizzBuzz(num) {\n    let res = ''\n    if(typeof num !== 'number') {\n        return false\n    }\n    res = (num % 5 === 0 && num % 3 === 0) ? 'fizzbuzz' : (num % 5 === 0) ? 'buzz' : (num % 3 === 0) ? 'fizz' : num\n    return res \n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 878ms, 内存消耗: 77856KB, 提交时间: 2020-12-28

{"css":"","js":"function fizzBuzz(num) {\n    if(num%3==0 && num%5==0){\n        return 'fizzbuzz';\n    }else if(num%3==0){\n        return 'fizz';\n    }else if(num%5==0){\n        return 'buzz';\n    }else if(!num || Object.prototype.toString.call(num) !== '[object Number]'){\n        return false;\n    }else{\n        return num;\n    }\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 880ms, 内存消耗: 77872KB, 提交时间: 2020-12-05

{"css":"","js":"function fizzBuzz(num) {\n    if(typeof(num) == \"number\"){\n        if(num%3===0 && num%5===0){\n        return \"fizzbuzz\"\n    }else if(num%3 === 0){\n        return \"fizz\"\n    }else if(num%5===0){\n        return \"buzz\"\n    }else{\n        return num\n    }\n    }else{\n        return false\n    }\n    \n}","html":"","libs":[]}

上一题