Skip to content

Instantly share code, notes, and snippets.

@ajchambeaud
Created December 2, 2017 18:05
Show Gist options
  • Select an option

  • Save ajchambeaud/6427a270cb4ef6f5d505e717c48a3a23 to your computer and use it in GitHub Desktop.

Select an option

Save ajchambeaud/6427a270cb4ef6f5d505e717c48a3a23 to your computer and use it in GitHub Desktop.

Revisions

  1. ajchambeaud created this gist Dec 2, 2017.
    35 changes: 35 additions & 0 deletions FizzBuzz.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /*
    * Write a program that prints the numbers from 1 to 100.
    * But for multiples of three print “Fizz” instead of the number
    * and for the multiples of five print “Buzz”.
    * For numbers which are multiples of both three and five print “FizzBuzz”
    */

    const range = (init, end) => {
    const nums = [];

    for(let i = init; i <= end; i ++) {
    nums.push(i);
    }

    return nums;
    };

    const multipleOf = (num, x) => x % num === 0;

    const printNumber = num => {
    if(multipleOf(3, num) && multipleOf(5, num))
    return console.log("FizzBuzz");

    if(multipleOf(5, num))
    return console.log("Buzz");

    if(multipleOf(3, num))
    return console.log("Fizz");

    console.log(num);
    }

    const nums = range(1, 100);

    nums.forEach(n => printNumber(n));