Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function fizzBuzz() {
- // Generate an array with 100 undefined items
- // iterate over array and build string of fizzbuzzes
- // alternately, use .forEach and log result each iteration
- const fizzBuzzResult = Array.apply(null, { length: 100 })
- .reduce( (accumulator, elem, i) => {
- let result = "";
- if ((i % 3) === 0) result += "Fizz"
- if ((i % 5) === 0) result += "Buzz";
- if (result !== "") accumulator += `${i}: ${result}!\n`;
- return accumulator;
- }, "");
- console.log(fizzBuzzResult);
- }
- fizzBuzz();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement