Home > Blockchain >  i want below code to print for reverse way like 10 to 0
i want below code to print for reverse way like 10 to 0

Time:01-04

for (let i =  1; i <=  10; i  ) {
  setTimeout(() => { console.log(i) }, (i * 1000) )
}

for (let i =  1; i <=  10; i  ) {
setTimeout(() => { console.log(i) }, (i * 1000) )
}

CodePudding user response:

maybe this?

for (let i =  10; i >=  1; i--)

or this:

for (let i =  1; i <=  10; i  ) {
  setTimeout(() => { console.log(10-i) }, (i * 1000) )
}

CodePudding user response:

you can just do this

for (let i =  0; i <  10; i  ) {
  setTimeout(() => { console.log(10 - i) }, (i * 1000) )
}

CodePudding user response:

You can do:

for (let i = 10; i >= 0; i--) {
  setTimeout(() => console.log(i), (10 - i) * 1000)
}

And here a more functional solution:

const logReversedNumbers = (n, ms) => Array.from(
  { length: n   1 }, (_, i) => setTimeout(() => console.log(i), (n - i) * ms)
)

logReversedNumbers(10, 1000)

  • Related