Skip to content

Instantly share code, notes, and snippets.

@wle8300
Last active November 9, 2016 21:14
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 wle8300/b674ab099ebb6dd31ab54cfe83b7dc20 to your computer and use it in GitHub Desktop.
Save wle8300/b674ab099ebb6dd31ab54cfe83b7dc20 to your computer and use it in GitHub Desktop.
Fizzbuzz for JavaScript
function fizzbuzz (set) {
var set = set ? set : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
var isValidSet = set.map((element) => {if (typeof element !== 'number') {return false} else return true}).indexOf(false) === -1 ? true : false
var gotFizz = (n) => {if (n % 3 === 0) {return true} else return false}
var gotBuzz = (n) => {if (n % 5 === 0) {return true} else return false}
if (!Array.isArray(set)) return new Error('First argument must an array with "Number" elements')
if (!isValidSet) return new Error('The elements of the first argument must all be "Numbers"')
set.forEach((n) => {
if (gotFizz(n) && gotBuzz(n)) return console.log('fizzbuzz')
if (gotFizz(n)) return console.log('fizz')
if (gotBuzz(n)) return console.log('buzz')
else return console.log(n)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment