Home>

If i run the code below, i ++ will only turn 4 and 1. What should I do to turn from 5 to 0 in order? I would appreciate your teaching.
Thank you.

function myFunction () {
  var count = 0;
 for (var i = 5;i>= 0;--i) {
count = i--;
  Logger.log (i--);
      }
}
  • Answer # 1

    for (var i = 5;i>= 0;--i) {// where -1
    count = i--;// where -2
      Logger.log (i--);// // -3 here
          }
    }

    The program does not work as you think
    It works as written

  • Answer # 2

    i ++doesn't appear, but it means that only 4 and 1 appear in the log.

    If you keep this count, count is too wasteful, so maybe you want to change this

    function myFunction () {
      var count = 0;
      for (var i = 5;i>= 0;--i) {
        count = i;
        Logger.log (count);
      }
    }

  • Answer # 3

    What is the program intended to do?
    Do you understand that --i is the same as i = i-1?
    count = i--;
    Logger.log (i--);
    As a result, the value of i is decreased by 2.
    count = i-1
    Logger.log (i-2)
    Should be like this. (I don't know the program's intent, so I don't know if it's the desired behavior.)

  • Answer # 4

    Decrement inside for is unnecessary.

Related articles