Created
March 20, 2012 04:49
-
-
Save FrancoB411/2131458 to your computer and use it in GitHub Desktop.
Fizzbuzz with recursion instead of a loop.
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(num) { | |
if(num === 0) { //base case | |
return 1; | |
}if(num < 0) { //termination case | |
return console.log("Positive numbers only, please."); | |
}if((num % 3 === 0) && (num % 5 === 0)) { //fizzbuzz conditionals | |
console.log("fizzbuzz"); | |
}else if (num % 3 === 0) { | |
console.log("fizz"); | |
}else if (num % 5 === 0) { | |
console.log("buzz"); | |
}else { | |
console.log(num); | |
} | |
fizzbuzz(num-1); //recursion | |
} | |
fizzbuzz(100); //starts the party |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment