Last active
March 30, 2022 13:01
-
-
Save AnkitMaheshwariIn/cdd4417e29c07f5798e48db313316f18 to your computer and use it in GitHub Desktop.
unction to count down numbers to 1 in JavaScript
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 to count down numbers to 1 | |
function countDownFunc(number) { | |
// display the number | |
console.log(number); | |
// decrease the number value by 1 | |
const newNumber = number - 1; | |
// check condition, the countDownFunc will call itself till the value of newNumber becomes 0 | |
if (newNumber > 0) { | |
// calling function itself with new number | |
countDownFunc(newNumber) | |
} else { | |
console.log(`Count down ends because the newNumber becomes ${newNumber}`) | |
} | |
} | |
console.log("Count down begins!") | |
// call a function with given number 5 | |
countDownFunc(5) | |
/* | |
THE OUTPUT WILL BE: | |
"Count down begins!" | |
5 | |
4 | |
3 | |
2 | |
1 | |
"Count down ends because the newNumber becomes 0" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment