Created
November 8, 2016 13:00
-
-
Save jcook3195/a7ea7b97594fb85bc9961f756f07dc5c to your computer and use it in GitHub Desktop.
JS FizzBuzz Solutions
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
//DRY option | |
function fizzBuzz() { | |
var output; | |
for(var i=1; i<=20; i++;) { | |
output=''; | |
if(i%3 === 0){ | |
output+='Fizz'; | |
} | |
if(i%5 === 0){ | |
output+='Buzz'; | |
} | |
if(output === '') { | |
output+=i; | |
} | |
console.log(output); | |
}; | |
}; |
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
//DRY with parameters | |
function fizzBuzz(end, fizzOutput, buzzOutput) { | |
var output; | |
for(var i=1; i<=end; i++) { | |
output = ''; | |
if(i%fizzOutput === 0){ | |
output+='Fizz'; | |
} | |
if(i%buzzOutput === 0){ | |
output+='Buzz'; | |
} | |
if(i%fizzOutput !== 0 && i%buzzOutput !== 0) { | |
output+=i; | |
} | |
console.log(output); | |
}; | |
}; | |
fizzBuzz(20, 3, 5); |
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
//Else If Statement | |
for(i=1; i<=20; i++) { | |
if(i%5 === 0 && i%3 === 0) { | |
console.log("FizzBuzz"); | |
} else if(i%5 === 0) { | |
console.log("Buzz"); | |
} else if(i%3 === 0) { | |
console.log("Fizz"); | |
} else { | |
console.log(i); | |
}; | |
}; |
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
//Switch Statement | |
for(i=1; i<=20; i++) { | |
switch(true) { | |
case(i%5 === 0 && i%3 === 0): | |
console.log("FizzBuzz"); | |
break; | |
case(i%5 === 0): | |
console.log("Buzz"); | |
break; | |
case(i%3 === 0): | |
console.log("Fizz"); | |
break; | |
default: | |
console.log(i); | |
break; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment