javascript - FizzBuzz textContent issue -
i'm using old fizzbuzz exercise, , utilizing textcontent, trying load page each of values listed 1 after other, vertically. right i'm getting instead:
12fizz4buzzfizz78fizzbuzz11fizz1314fizzbuzz1617fizz19buzzfizz2223fizzbuzz26fizz2829fizzbuzz3132fizz34buzzfizz3738fizzbuzz41fizz4344fizzbuzz4647fizz49buzzfizz5253fizzbuzz56fizz5859fizzbuzz6162fizz64buzzfizz6768fizzbuzz71fizz7374fizzbuzz7677fizz79buzzfizz8283fizzbuzz86fizz8889fizzbuzz9192fizz94buzzfizz9798fizzbuzz
here code:
var n = 1, str = "" while (n <= 100) { if (n % 3 === 0 && n % 5 === 0) { str = str + "fizzbuzz" } else if (n % 3 === 0) { str = str + "fizz" } else if (n % 5 === 0) { str = str + "buzz" } else { str = str + n } n++ } document.queryselector(".container").textcontent = str
<!doctype html> <html> <head> </head> <body> <p class="container"></p> </body> </html>
don't use textcontent
, since doesn't include formatting. use innerhtml
, put <br>
tags after each item.
var n = 1, str = "" while (n <= 100) { if (n % 3 === 0 && n % 5 === 0) { str = str + "fizzbuzz<br>"; } else if (n % 3 === 0) { str = str + "fizz<br>"; } else if (n % 5 === 0) { str = str + "buzz<br>"; } else { str = str + n + '<br>'; } n++; } document.queryselector(".container").innerhtml = str
<!doctype html> <html> <head> </head> <body> <p class="container"></p> </body> </html>
Comments
Post a Comment