#412 Fizz Buzz

题目:

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

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;

};

你可能感兴趣的:(#412 Fizz Buzz)