I have been tasked with looping through an array using the .forEach method in order to return all the values. If any of the numbers are a multiple of 3 or 5, or both 3 and 5, that number must be returned as a string. Here is my current code:
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15];
let count = [];
arr.forEach((item) => {
count[item] = item;
if (count[item] % 3 === 0) {
count[item] = "Fizz";
} else if (count[item] % 5 === 0) {
count[item] = "Buzz";
} else if (count[item] % 3 === 0 || count[item] % 5 === 0) {
count[item] = "FizzBuzz";}
return count;});
console.log(count);
The challenge I am facing is that I am struggling to get it to return the final string "FizzBuzz" and also prevent the initialization of the count variable from displaying in the console.
As a beginner in JS, I would appreciate an explanation of the solution wherever possible.
Attached below are images of the outputs:
Edit:
I should mention that this exercise is part of an assignment, so I must use the forEach method. This sets it apart from another question where a for loop was utilized instead.