Your solution is good. One big change your code could use is to simplify into a single loop. Generating an array counts as the first loop, of course. Replacing it with a for loop or a while loop works well for this application. You already have a for loop, so let's reconstruct your fizzBuzz function to harness that.
Remove the array and the populateArray function
Removing the fizzBuzzNumbers array and the accompanying function will simplify the rest of the function drastically.
const fizzBuzz = (num) => { for (let i = 0; i < fizzBuzzNumbers.length; i++) { if (fizzBuzzNumbers[i] % 3 === 0 && fizzBuzzNumbers[i] % 5 === 0) { console.log("FizzBuzz"); } else if (fizzBuzzNumbers[i] % 3 === 0 && fizzBuzzNumbers[i] % 5 !== 0) { console.log("Fizz"); } else if (fizzBuzzNumbers[i] % 3 !== 0 && fizzBuzzNumbers[i] % 5 === 0) { console.log("Buzz"); } else { console.log(fizzBuzzNumbers[i]); } } };
Refactor the old array references
So, we want to change fizzBuzzNumbers.length to num to use the num parameter. I'll go ahead and change num to max for clearer understanding of its intent. I'll change the initialization of the for loop to i = 1 to start at 1 as intended and the condition to i <= max.
const fizzBuzz = (max) => { for (let i = 1; i <= max; i++) { if (fizzBuzzNumbers[i] % 3 === 0 && fizzBuzzNumbers[i] % 5 === 0) { console.log("FizzBuzz"); } else if (fizzBuzzNumbers[i] % 3 === 0 && fizzBuzzNumbers[i] % 5 !== 0) { console.log("Fizz"); } else if (fizzBuzzNumbers[i] % 3 !== 0 && fizzBuzzNumbers[i] % 5 === 0) { console.log("Buzz"); } else { console.log(fizzBuzzNumbers[i]); } } };
Additionally, all references to fizzBuzzNumbers[i] can simply be changed to i instead:
const fizzBuzz = (max) => { for (let i = 1; i <= max; i++) { if (i % 3 === 0 && i % 5 === 0) { console.log("FizzBuzz"); } else if (i % 3 === 0 && i % 5 !== 0) { console.log("Fizz"); } else if (i % 3 !== 0 && i % 5 === 0) { console.log("Buzz"); } else { console.log(i); } } };
Nit-picky suggestions
This code works just as your original code does, but there are a few nit-picky things that could be adjusted.
If a number is divisible by both 3 and 5, you can reduce the condition to i % 15 === 0:
const fizzBuzz = (max) => { for (let i = 1; i <= max; i++) { if (i % 15 === 0) { console.log("FizzBuzz"); } else if (i % 3 === 0 && i % 5 !== 0) { console.log("Fizz"); } else if (i % 3 !== 0 && i % 5 === 0) { console.log("Buzz"); } else { console.log(i); } } };
The negation checks when a number is divisible by either 5 or 3 (exclusive) can be removed (they are implied):
const fizzBuzz = (max) => { for (let i = 1; i <= max; i++) { if (i % 15 === 0) { console.log("FizzBuzz"); } else if (i % 3 === 0/* && i % 5 !== 0 */) { console.log("Fizz"); } else if (/* i % 3 !== 0 && */i % 5 === 0) { console.log("Buzz"); } else { console.log(i); } } };
Keep practicing! Make sure to keep your code readable even if it's just for yourself (variable names, code indentation, comments, etc).
i = 1and then drop the array completely. \$\endgroup\$I'm not entirely sure if my program works completely, but I think it does.How do you come to be unsure? Have you run the program? Did it work? Have you run it for some other ranges? Have you considered what ranges might cause weird behaviours? Welcome to Software development. More than half of the work is testing and debugging the code you wrote :D \$\endgroup\$