javascript - How can we increment and then decrement a counter without conditionals? -


I'm looking for a clever way to increase and then to reduce a counter variable. I want the counter variable to start with lower value and increments, towards a higher value. Once it reaches the higher value, the counter decreases until it reaches the lower value. Once it reaches the lesser value, then the counter increments for more value again ... I think you get the point.

I am working on it where I want to wish some tricks, code> if or other conditional test:

Here is the conditional argument that handles the counter variable Is:

  incremental = true foo = - & gt; Length = = 1 If the length of increment - = 1 if incrementing = false incrementing, then & gt; 100 incrementing = true if length & lt; 1  

Initially, I thought I could use modules. However, the modulo deviation only reset the counter to a lower value - it does not decrease the counter after reaching the top value.

  0% 10 = 0 1% 10 = 1 2% 10 = 2 3% 10 = 3 4% 10 = 4 5% 10 = 5 6% 10 = 6 7% 10 = 7 8% 10 = 8 9% 10 = 9 10% 10 = 0 11% 10 = 1 12% 10 = 2 13% 10 = 3  

I'm sure using conditional tests There should be a way to do without it. Let's say a value of 0 and the top value of 10, the method must be in the following.

 ? = 0? = 1? = 2? = 3? = 4? = 5? = 6? = 7? = 8? = 9? = 8? = 7? = 6? = 5  

First look at some waves!

Hello waves!

types of waves

Do you have your needs Can you identify who is matching?

If you guess , then you are right!

Triangle

Two things to work here:

      Li> The origin of the ceremony will start in the middle of the wave
    • wave x to -x

    We want to start the output on 0 and it only contains positive values ​​

      function triangle (T, A) {return Math.abs (( (T + A / 2)% A) - A / 2); }  

    Try it out

     for  (var i = 0; i & lt; 20; i ++) {console.log} (I, triangle (i, 10)); }  

    Output

      0 1 2 2 3 3 4 4 5 5 6 4 7 3 8 2 9 1 10 0 11 1 12 2 13 3 14 4 15 5 16 4 17 3 18 2 1 1  

    So when we call triangle (i, 10) the 10 "Duration" is

    a duration 6 us 6 value, 0, 1, 2, 3, 2, 1

    The duration of 4 will give us 4 values, 0, 1, 2, 1 etc.

    if you enter 0-9 If you want to go, you will need a duration of 20


Comments