Javascript:仅返回一个空数组

我正在尝试解决fizzbuzz的问题,该问题返回了应计整数和字符串的数组,但是return只返回一个空数组。我可以将数组打印到控制台上,并且效果很好(即,已完成所有我要的更改)。我在概念上想念什么?

码:

let fizzbuzz = [];

for (let i =1; i<=100; i++){

    let by3 = i%3==0; // i is divisble by 3
    let by5 = i%5==0; // i is divisible by 5
    let output="";


    if (by3){output+="Fizz";}
    if (by5){output+="Buzz";}
    if (output==="") {output = i;}
    fizzbuzz.push(output);
}

//console.log(fizzbuzz); 

return fizzbuzz; // will return an empty array. But why? 
评论
香思松
香思松

You can only do return inside functions.

点赞
评论