As I work on refining a solution for fizzbuzz to generate an array of numbers and strings, I encountered an issue where the return statement only outputs an empty array. Interestingly, when I print the array to the console, it appears as intended with all the modifications I made. What could be the underlying concept that I am overlooking in this scenario?
Here is the code snippet:
function display(){
let fizzbuzz = [];
for (let i =1; i<=100; i++){
let by3 = i%3==0; // checks if i is divisible by 3
let by5 = i%5==0; // checks if 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; // the return statement is returning an empty array. Why is this happening?
}
display();