3 ms·
Clever: for(let i=0;i<100;)console.log((++i%3?'':'fizz')+(i%5?'':'buzz')||i) Readable: for (let i = 1; i <= 100; i++) { if (i % 3 == 0 && i % 5 =
by DonCopal 7y ago
Clever:
for(let i=0;i<100;)console.log((++i%3?'':'fizz')+(i%5?'':'buzz')||i)
Readable:
for (let 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);
}
}
- blumomo 7y agoI like that example. Since I'm preaching to prefer functional style programing and to separate the side effect from the rest of the algorithm, I'd write the code this way: Array .from({length: 100}, (v, k) => k+1) .map(i => { if (i % 3 == 0 && i % 5 == 0) return "FizzBuzz" if (i % 3 == 0) return "Fizz" if (i % 5 == 0) return "Buzz" return i }) .forEach(console.log)