美文网首页
#412 Fizz Buzz

#412 Fizz Buzz

作者: 乐正君Krizma | 来源:发表于2017-04-27 21:47 被阅读0次

题目:

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.


代码:

/**

* @param {number} n

* @return {string[]}

*/

var fizzBuzz = function(n) {

var res = [];

function five (n) {

if(n%5 === 0) {

return true;

}

return false;

}

function three (n) {

if(n%3 === 0) {

return true;

}

return false;

}

for(var i=0;i<n;++i){

var t = i+1;

if(three(t) && !five(t)) {

res[i]='Fizz';

}

if(!three(t) && five(t)) {

res[i]='Buzz';

}

if(three(t) && five(t)) {

res[i]='FizzBuzz'

}

if(!three(t) && !five(t)) {

res[i]=t.toString()

}

}

return res;

};

相关文章

  • LeetCode 411-430

    412. Fizz Buzz[https://leetcode-cn.com/problems/fizz-buzz...

  • Leetcode PHP题解--D40 412. Fizz Bu

    412. Fizz Buzz 题目链接 412. Fizz Buzz 题目分析 这个题目也很简单。 从1逐个输出到...

  • Leetcode 412 Fizz Buzz

    题目链接:412. Fizz Buzz 题目描述 Write a program that outputs the...

  • #412 Fizz Buzz

    题目: Write a program that outputs the string representatio...

  • 412. Fizz Buzz

    一、题目原型: 写一个程序,输出从 1 到 n 数字的字符串表示。 如果 n 是3的倍数,输出“Fizz”;如果 ...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

    题目分析 题目链接,登录 LeetCode 后可用思路比较简单,直接遍历1 到 n 的每个数,依次判断每种情况即可...

  • 412. Fizz Buzz

    Write a program that outputs the string representation of...

  • 412. Fizz Buzz

网友评论

      本文标题:#412 Fizz Buzz

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