Is it a bug right?

Problem description

function cycleGo() {
  let str = "";
  for (let i = 20; i == 0; i = 4) {
    str = str - i;
  return str + "Run!";

console.log inside the loop does not print anything in a console.
Did I make everything correct?

Expected behavior

console.log should print everything on each step of a loop iteraction a value.

Actual behavior

Nothing happen

Steps to reproduce

Just Run code inside replit.




Mac OS

Device if mobile




No, that’s expected behavior. The for loop is never started, since you start with i=20 which doesn’t satisfy i==0, so it’s skipped. You only log an empty string, which is what happens on my side too.

I think your loop is badly written, the way it works in Javascript is

for(start, condition, increment) {
  // Do stuff

Where start is executed once before the loop starts, condition is checked before each run, and if it resolves to false then the loop is exited, and increment is executed after the loop.

If your goal is to run something n times, you should make your for loop like this:

for(let i = 0; i < n; i++) {
    // Do stuff

If you want it to be in descending order, you need to make it the opposite:

for(let i = n - 1; i >= 0; i--) {
    // Do stuff

(n-1 is because you end at 0, not 1)

I hope this helps, feel free to reply if you need further help