Skip to content

Instantly share code, notes, and snippets.

@jsstrn
Last active November 7, 2015 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsstrn/3588e46165319dad163f to your computer and use it in GitHub Desktop.
Save jsstrn/3588e46165319dad163f to your computer and use it in GitHub Desktop.
A FizzBuzz solution written in JavaScript
/**
* 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