JavaScript arrays and loops combination question

  • Java
  • Thread starter mindauggas
  • Start date
  • #1
127
0

Main Question or Discussion Point

var lost = [4, 8, 15, 16, 23, 42];
var count = lost.length;

var isLost = function (n) {
for (i=lost[0]; i<count; i=lost[i+1]) {
if ( n === lost) {
return "true";
}
}
return "false";
};

is it possible to in corporate array positions into the for loop? (i)
do I even begin to approach the correct "incorporation"? (ii)
 

Answers and Replies

  • #2
Filip Larsen
Gold Member
1,237
169
As I understand your questions:
(i) Use i = i+1, or i++ as the increment.
(ii) You would want to return the boolean literals true or false instead of their string representation. You may also want to check out the array indexOf operation [1].

[1] http://www.w3schools.com/jsref/jsref_indexof_array.asp
 
  • #3
Borg
Science Advisor
Gold Member
1,871
2,220
You should do something like this to read the contents of an array. Don't forget to declare var i and n (whatever that is):
Code:
for(var i = 0; i < lost.length; i++){
    if(n == lost[i]){
        return true;
    }
}
is it possible to in corporate array positions into the for loop? (i)
do I even begin to approach the correct "incorporation"? (ii)
var i in the for loop is the position of the array that you are examining.
 
  • #4
127
0
Thanks.
 

Related Threads for: JavaScript arrays and loops combination question

  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
1K
Replies
9
Views
796
  • Last Post
Replies
1
Views
1K
Replies
2
Views
4K
  • Last Post
Replies
3
Views
3K
Top