#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<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;

};

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容