3 ms·
JavaScript can eliminate all repetition thanks to how its || operator works: for (let i = 1; i <= 100; i++) { console.log( (i % 3 == 0 ? "Fiz
by Zarel 5y ago
JavaScript can eliminate all repetition thanks to how its || operator works:
for (let i = 1; i <= 100; i++) {
console.log(
(i % 3 == 0 ? "Fizz" : "") + (i % 5 == 0 ? "Buzz" : "")
|| i
);
}
Whether or not you consider this more elegant is a matter of taste, though.
- strogonoff 5y agoWhile I like to think the opposite, I can see myself using || with an empty string occasionally (at least with TS, where I can be reasonably sure the variable will definitely be a string at runtime). However, concatenating strings with + is a bit implicit to my taste.