Last active
November 9, 2016 21:14
-
-
Save wle8300/b674ab099ebb6dd31ab54cfe83b7dc20 to your computer and use it in GitHub Desktop.
Fizzbuzz for JavaScript
This file contains hidden or 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
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