Last active
November 7, 2015 05:54
-
-
Save jsstrn/3588e46165319dad163f to your computer and use it in GitHub Desktop.
A FizzBuzz solution written in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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". | |
* */ | |
var size = 100 | |
method1(size) | |
method2(size) | |
method3(size) | |
function method1 (size) { | |
for (var i = 1; i <= size; i++) { | |
if (i % 15 === 0) { | |
console.log('fizzbuzz') | |
} else if (i % 3 === 0) { | |
console.log('fizz') | |
} else if (i % 5 === 0) { | |
console.log('buzz') | |
} else { | |
console.log(i) | |
} | |
} | |
} | |
function method2 (size) { | |
for (var i = 1; i <= size; i++) { | |
i % 15 === 0 ? console.log('fizzbuzz') | |
: i % 5 === 0 ? console.log('buzz') | |
: i % 3 === 0 ? console.log('fizz') | |
: console.log(i) | |
} | |
} | |
function method3 (size) { | |
var array = [] | |
for (var i = 0; i < size; i++) { | |
array[i] = array.push(i) | |
} | |
array.forEach(function (i) { | |
i % 15 === 0 ? console.log('fizzbuzz') | |
: i % 5 === 0 ? console.log('buzz') | |
: i % 3 === 0 ? console.log('fizz') | |
: console.log(i) | |
}) | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment