-
-
Save airbr/8b638b95e5943c0fe98d372f7f38bdf0 to your computer and use it in GitHub Desktop.
Fizzbuzz with recursion instead of a loop.
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
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