count down in javascript

To count down in javascript, you can use a loop, a variable to store the current count value, and conditionals to check when the count is finished. Here is an example using a for loop:

index.tsx
for (let i = 10; i >= 0; i--) {
  console.log(i);
}
52 chars
4 lines

This loop will start at 10 and count down to 0, logging each count value to the console. You can also use a while loop with a variable to keep track of the count:

index.tsx
let count = 10;
while (count >= 0) {
  console.log(count);
  count--;
}
72 chars
6 lines

This code will have the same result as the example above. To make this countdown more modular, you can create a function that takes a starting value and counts down to 0:

index.tsx
function countdown(start) {
  for (let i = start; i >= 0; i--) {
    console.log(i);
  }
}

countdown(5); // Will output 5, 4, 3, 2, 1, 0
138 chars
8 lines

gistlibby LogSnag