Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Returning Index (http://www.programmingforums.org/showthread.php?t=14721)

littlejim4 Dec 10th, 2007 1:52 AM

Returning Index
 
Hi all,

I have a bit of a problem with a project and was hoping that somebody could help me out. Here goes!

I have a set of integers ie (1,-2,0,4,-5,0,7,-8,0,10) what I need to do is loop through and then return only the positive integers leaving out the negatives. This is easy enough but I have to return the index positions of the positive integers ie, (1,3,4,6,7,9,10). However I'm having major trouble with the logic because I need the integers in reverse order. What I get is the index integers from 1-7 not waht I need.

Can anybody help me out with this one. Many thanks in advance.

hollystyles Dec 10th, 2007 3:07 AM

Re: Returning Index
 
Just loop the array of integers in reverse.

:

var integerArray = [1,-2,0,4,-5,0,7,-8,0,10];
var positiveInts = [];
for(var i = integerArray.length - 1, i >= 0, --i)
{
    if(integerArray [i] >= 0)
        positiveInts[positiveInts.length] = integerArray[i];
}


littlejim4 Dec 10th, 2007 8:39 AM

Re: Returning Index
 
Many thanks,

I was getting bogged down by the logic.


All times are GMT -5. The time now is 9:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC