Advertisement
CLooker

FizzBuzz fun

Feb 7th, 2018
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.from(Array(100)).forEach((_, index) => {
  2.   let fizzBuzzStr = "";
  3.   (index + 1) % 3 === 0 && (fizzBuzzStr += "Fizz");
  4.   (index + 1) % 5 === 0 && (fizzBuzzStr += "Buzz");
  5.   console.log(fizzBuzzStr || index + 1);
  6. });
  7.  
  8. Array.from(Array(100))
  9.   .reduce(
  10.     ({ fizzBuzzes, fizzBuzzStr, j }, _, i) => (
  11.       j % 3 === 0 && (fizzBuzzStr += "Fizz"),
  12.       j % 5 === 0 && (fizzBuzzStr += "Buzz"),
  13.       { fizzBuzzes: [...fizzBuzzes, fizzBuzzStr || j], fizzBuzzStr: "", j: ++j }
  14.     ),
  15.     { fizzBuzzes: [], fizzBuzzStr: "", j: 1 }
  16.   )
  17.   .fizzBuzzes.forEach(fizzBuzz => console.log(fizzBuzz));
  18.  
  19. Array.from(Array(100))
  20.   .reduce(
  21.     ({ fizzBuzzes, fizzBuzzStr, j }, _, i) => {
  22.       if (j % 3 === 0) {
  23.         fizzBuzzStr += "Fizz";
  24.       }
  25.       if (j % 5 === 0) {
  26.         fizzBuzzStr += "Buzz";
  27.       }
  28.       return {
  29.         fizzBuzzes: [...fizzBuzzes, fizzBuzzStr || j],
  30.         fizzBuzzStr: "",
  31.         j: ++j
  32.       };
  33.     },
  34.     { fizzBuzzes: [], fizzBuzzStr: "", j: 1 }
  35.   )
  36.   .fizzBuzzes.forEach(fizzBuzz => console.log(fizzBuzz));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement