I am trying to figure out what I am doing wrong with this problem. I seem to be missing something that is not allowing my code to work. I need to use a function to create an array that takes one number n, and that loops through an array from the numbers 1 - 16, while also replacing all the numbers divisible by 3 with the string 'fizz', and all divisible by 5 with the word 'buzz', and any number that is divisible by both must be replaced by the string 'fizzbuzz'.
I have gone over this several times but for some reason keep coming up with just an empty array for my result when it is logged to the console. I would greatly appreciate it if someone could give me some tips as to why my code isn't working so that I can understand the concept easier. Here is my code:
const results = [];
const fizzbuzz = (n) => {
var results = []
for (let n = 1; results.length < 16; n++) {
if (n % 3 === 0 && n % 5 === 0) {
results.push('FizzBuzz')
} else if (n % 3 === 0) {
results.push('fizz');
} else if (n % 5 === 0) {
results.push('buzz')
} else {
results.push(n)
}
}
return results
};
fizzbuzz(16);
console.log(results);
This is what it is supposed to come out to:
[1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16]
But this is what I keep getting as an answer:
[]