I am new to programming and am having a go at doing my own FizzBuzz challenge which is the following:
"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."
I am doing it in Javascript & jQuery and I can get it to print in the console but how do I modify it so that it prints it actually onto the page itself? Like maybe using HTML to help output a list/array or something so you can see what would be in the console, on the page, if I'm making sense?
At the moment I have got it working in the console by doing this:
for (var i=1; i < 101; i++){
if (i % 15 == 0) console.log("FizzBuzz");
else if (i % 3 == 0) console.log("Fizz");
else if (i % 5 == 0) console.log("Buzz");
else console.log(i);
}
and then I changed it to this:
<p id="fizzbuzz"></p>
for (var i=1; i < 101; i++){
if (i % 15 == 0) {
$('#fizzbuzz').html("FizzBuzz");
}
else if (i % 3 == 0) {
$('#fizzbuzz').html("Fizz");
}
else if (i % 5 == 0) {
$('#fizzbuzz').html("Buzz");
}
else console.log(i);
}
But this is obviously only printing out one line as it is replacing the text within the 'fizzbuzz'
ID instead of creating a list of the loop outputs.
Can anyone help me, please? Sorry I'm a noob, I'm trying to learn.
Thanks.