How to reverse the order in a FOR loop

前端 未结 12 532
滥情空心
滥情空心 2020-12-31 06:16

I\'ve a simple FOR statement like this:

var num = 10,
    reverse = false;

for(i=0;i

when rever

相关标签:
12条回答
  • 2020-12-31 06:30

    Roy's is similar to mine, but here's what I was thinking. I'll give you what I wrote in C# and how I think it translates to Javascript.

    C#

        int num = 10;
        bool reverse = true;
    
        for (int i = reverse ? num : 0; (reverse ? 0 : i) < (reverse ? i : num); i += reverse ? -1 : 1)
        {
            Console.Write((reverse ? i - 1 : i).ToString());
        }
        Console.ReadKey();
    

    Javascript

            var num = 10,
            reverse = true;
    
        for (int i = reverse ? num : 0; (reverse ? 0 : i) < (reverse ? i : num); i += reverse ? -1 : 1)
        {
            console.log(reverse ? i - 1 : i);
        }
    

    And here's another way
    Javascript

        var num = 10,
            reverse = false;
    
        for (int i = 0; i < num; i++)
        {
          console.log((reverse ? abs(-num + (i + 1)) : i));
    
        }
    
    0 讨论(0)
  • 2020-12-31 06:30

    It seems to work:

      var num = 10;
      var z = 1;
      var k = -10;
      if (reverse ){
        k = -1;
        z = -1;
      }
      for(int i = 0; i < 10; i++){
        console.log(num+i*z+k);
      }
    
    0 讨论(0)
  • 2020-12-31 06:35

    Surely in a language like Javascript there must be a way to define a local function and use that in the loop?

    function SubtractFrom(val, subtractor) {
        return val - subtractor;
    }
    
    function PassThrough(val) {
        return val;
    }
    
    var num = 10;
    var processor = reverse ? SubtractFrom(num-1) : PassThrough;
    
    for (i = 0; i < num; i++) {
        console.log(processor(i));
    }
    

    Not knowing Javascript though, I don't know what actual form the function definitions would take.

    0 讨论(0)
  • 2020-12-31 06:38

    I think this meets your requirements:

    var num = 10;
    var reverse = false;
    var diff = 0;
    
    if (reverse) {
        diff = num - 1;
    }
    
    for (i = 0; i < num; i++) {
        console.log(Math.abs(diff - i));
    }
    
    0 讨论(0)
  • 2020-12-31 06:41
    var start; var end; var inc;
    if (reverse) {
        start = num-1; end = 0; inc = -1;
    }
    else {
        start = 0; end = num-1; inc = 1;
    }
    for(i=start;i!=end;i+=inc){
        console.log(i)
    }
    
    0 讨论(0)
  • 2020-12-31 06:41

    I just came across the need for this the other day. Here's how I did it:

    var num = 10,
        i = 0,
        direction = 1, 
        reverse = false;
    
    if(reverse)
        i = num + (direction = num = -1);
    
    for(; i !== num; i += direction) {
        console.log(i);
    }
    

    No need for separate loops, and no need to do math to calculate the proper i in the loop.

    So if reverse is true...

    • i (which represents our first item) becomes num - 1, so we're now starting on what would have been the last item

    • num (which represents out of bounds) becomes -1, so we're now stopping on what would have been the first item

    • direction is -1, which means it will decrement when we do i += direction

    So by swapping our starting point with our ending point and changing the alteration of i from 1 to -1, we'll be going up or down based on those modifications.

    0 讨论(0)
提交回复
热议问题