javascript - for loops and negative numbers - Stack Overflow

This is a very basic Javascript question. See, I have a for loop which starts like thisfor (i=0;i<=n

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.

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.

Share Improve this question edited Sep 27, 2014 at 21:12 tomysshadow asked Sep 27, 2014 at 21:03 tomysshadowtomysshadow 9601 gold badge9 silver badges23 bronze badges 5
  • 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 Commented Sep 27, 2014 at 21:05
  • Take the absolute value. – Code-Apprentice Commented Sep 27, 2014 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 Commented Sep 27, 2014 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 acplish? – Makoto Commented Sep 27, 2014 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 Commented Sep 27, 2014 at 21:24
Add a ment  | 

4 Answers 4

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

Documentation here.

Why not use Absolute value.

for(int i = 0; i < abs(positiveorneativenumber); i++) 
{
    //do stuff
}

similar to the above ments 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;
}

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/Guffa/cbh5c7rb/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745277777a4620134.html

相关推荐

  • javascript - for loops and negative numbers - Stack Overflow

    This is a very basic Javascript question. See, I have a for loop which starts like thisfor (i=0;i<=n

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信