-2

This is a very basic Javascript question. See, I have a for loop which starts like this

for (i=0;i<=negativeorpositivenumber;i++) {

As the name suggests, the positiveornegativenumber variable can contain either a positive or negative number. As a result, if said variable is positive, the for loop works. If it is negative, the loop exits immediately because i is already larger than negativeorpositivenumber.

Is there a way, without using two for loops (one for negative and one for positive) to have this work both ways? So change i<= to i>= and i++ to i-- if the variable negativeorpositivenumber is negative?

I suppose this question could also apply to more than one language as well.

EDIT: perhaps I should've made this more clear. I can't use absolute value because the value of i actually needs to count down. Thanks to all the people who were helpful and answered using Math.abs, however.

tomysshadow
  • 870
  • 1
  • 8
  • 23
  • 1
    you could use the javascript abs() to make sure it's always positive in any event like: for(i=0;i<=abs(negativeorpositivenumber);i++){ – Mark Fitzpatrick Sep 27 '14 at 21:05
  • Take the absolute value. – Code-Apprentice Sep 27 '14 at 21:05
  • Is your goal to get the correct number pf iterations, or dpes the actual value of i matter inside the loop? – Chris Tavares Sep 27 '14 at 21:07
  • 1
    This doesn't make sense. Why do you need i to count down in negative value cases? Do you care about the value of i? What are you trying to accomplish? – Makoto Sep 27 '14 at 21:14
  • Complicated. I have a grid, and two of the squares on the grid are lit. I want to connect those two by lighting up the space between them with two straight lines. The second square may either be to the left or right of the first, meaning the variable could be negative or positive, and so this loop must be able to cope with both. – tomysshadow Sep 27 '14 at 21:24

4 Answers4

2
for (i=0;i<=Math.abs(negativeorpositivenumber);i++) {
    var original = negativeorpositivenumber > 0 ? i : -i;    // optional

Documentation here.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • That still counts up for negative valiues of `negativeorpositive`. – Guffa Sep 27 '14 at 21:07
  • Aha! This is what I was looking for. Not only does the loop work, but it can also be altered so i counts down. Closest as it gets I guess. Thank you! – tomysshadow Sep 27 '14 at 21:14
0

Why not use Absolute value.

for(int i = 0; i < abs(positiveorneativenumber); i++) 
{
    //do stuff
}
  • According to the other answers, the function is called Math.abs in JS. But it still doesn't quite answer my question, because i needs to count down. – tomysshadow Sep 27 '14 at 21:15
0

similar to the above comments but based on the sign of the number, you can do a while loop like this:

var sign = negativeorpositivenumber? negativeorpositivenumber <0?-1:1:0
while(i != 0){
  i -= sign;
}
gabber
  • 100
  • 7
  • sign in javascript from http://stackoverflow.com/questions/7624920/number-sign-in-javascript – gabber Sep 27 '14 at 21:12
0

To loop in either direction, you can first check where you are going, and what the bounds are:

var dir = negativeorpositivenumber > 0 ? 1 : -1;
var min = negativeorpositivenumber > 0 ? 0 : negativeorpositivenumber;
var max = negativeorpositivenumber > 0 ? negativeorpositivenumber : 0;

for (var i = 0; i >= min && i <= max; i += dir) {
  ...
}

Demo: http://jsfiddle.net/Guffa/cbh5c7rb/

Guffa
  • 687,336
  • 108
  • 737
  • 1,005