Skip to content

Instantly share code, notes, and snippets.

@AnkitMaheshwariIn
Last active March 30, 2022 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnkitMaheshwariIn/cdd4417e29c07f5798e48db313316f18 to your computer and use it in GitHub Desktop.
Save AnkitMaheshwariIn/cdd4417e29c07f5798e48db313316f18 to your computer and use it in GitHub Desktop.
unction to count down numbers to 1 in JavaScript
// 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