The thread for this: https://www.kirupa.com/html5/fizzbuzz.htm
My solution is here:
<!DOCTYPE html>
<html>
<head>
<title>FizzBuzz</title>
</head>
<body>
<script>
function fizzBuzz() {
for (var i = 1; i <= 100; i++) {
if ((i % 3 == 0) && (i % 5 == 0)) {
console.log("FizzBuzz");
} else if (i % 3 == 0) {
console.log("Fizz");
} else if (i % 5 == 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
}
fizzBuzz();
</script>
</body>
</html>
One liner
Array.from(Array(100), (n, i) => (++i, (i % 3 == 0 ? 'Fizz' : '') + (i % 5 == 0 ? 'Buzz' : '') || i)).forEach(i => console.log(i))
1 Like
Haha! Nice
Lots of approaches and discussion here:
1 Like
Kirupa, my friend!
Your post is dated May 2018. Why not use
for (let i = 1; …)
instead of
for (var i = 1; …) ?
1 Like
Old habits and so on! Good point. I still find myself using var
almost exclusively unless I absolutely need the scoping improvements that let
provides
1 Like