I have a function here that replaces the elements that are multiples of 3 & 5 with "Fizz" and "Buzz" respectively. I ran the function and it only replaces the 1st element with "Fizz", and nothing else. I'm not sure why it only replaces the 1st element, since it doesn't match any of the if else statements that I have. I don't believe that I need to have the array within the function, do I, since it would mean that the array was being created while I was attempting to replace the elements.
var listOfNumbers = new Array(100);
var i, j;
for (i = 0; i < listOfNumbers.length; i++){
listOfNumbers[i] = i + 1;
}
function fizzBuzz(listOfNumbers){
for (j = 0; j < listOfNumbers.length; j++) {
if (j % 3 == 0 && j % 5 == 0){
listOfNumbers[j] = "FizzBuzz";
}
else if (j % 3 == 0) {
listOfNumbers[j] = "Fizz";
}
else if (j % 5 == 0) {
listOfNumbers[j] = "Buzz";
}
else {
listOfNumbers[j] = j;
}
return listOfNumbers;
}
document.getElementById("Nums").innerHTML = fizzBuzz();
}
I'm not sure
Also, I have a button to display the array, but it doesn't seem to display the array when clicking on it.
<h4>Fizz Buzz</h4>
<p>An idea that prints the elements of an array from 1 to 100, but it prints "Fizz" for every multiple of 3, "Buzz" for ever multiple of 5, and finally "FizzBuzz" for every multple of 15.
Idea taken from <a href="https://github.com/karan/Projects#text">here</a>.</p>
<button type="button" onclick="fizzBuzz()">Click to see the results</button>
<p id="Nums"></p>
returnin theforloop?returning inside the for loop, thus it exits after the first iteration, and doesn't apply to the entire array.fizzBuzzfunction"FizzBuzz"?listOfNumbers[j] = j;it makes an assumption which is not clear, because you have already values in it, but you change it later to a different value.