描述
给你一个整数n. 从 1 到 n 按照下面的规则打印每个数:
如果这个数被3整除,打印fizz.
如果这个数被5整除,打印buzz.
如果这个数能同时被3和5整除,打印fizz buzz.
样例
比如 n = 15, 返回一个字符串数组:
[
"1", "2", "fizz",
"4", "buzz", "fizz",
"7", "8", "fizz",
"buzz", "11", "fizz",
"13", "14", "fizz buzz"
]
挑战
Can you do it with only one if statement?
* @param n: An integer
* @return: A list of strings.
*/
const fizzBuzz = function (n) {
var result=[],i=1;
for(i;i<n+1;i++){
result.push(i+'');
}
for(i=2;i<n;){
result[i]='fizz';
i+=3;
}
for(i=4;i<n;){
if(result[i]=='fizz'){
result[i]='fizz buzz';
}else{
result[i]='buzz';
}
i+=5;
}
return result;
}
网友评论