![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2007
Location: South Wales
Posts: 6
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Omlette du fromage
Join Date: Oct 2007
Posts: 29
Rep Power: 0
![]() |
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];
} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2007
Location: South Wales
Posts: 6
Rep Power: 0
![]() |
Re: Returning Index
Many thanks,
I was getting bogged down by the logic. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Method not returning string | csrocker101 | C# | 1 | Apr 6th, 2007 6:45 PM |
| Returning An Array Of Characters | Sane | C++ | 22 | Aug 22nd, 2006 3:44 AM |
| Method not returning? | titaniumdecoy | Python | 6 | Jun 28th, 2006 2:19 PM |
| Index in a table | gj15987 | Delphi | 12 | Aug 22nd, 2005 7:02 PM |
| Finally returning.... | Sebeq | Community Introductions | 12 | Feb 23rd, 2005 11:33 AM |