JavaScript arrays and loops combination question

Click For Summary

Discussion Overview

The discussion revolves around a JavaScript function that checks if a number exists within an array. Participants explore the correct implementation of loops and array indexing in the context of this function.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant questions how to incorporate array positions into the for loop and seeks guidance on the correct approach.
  • Another participant suggests using increment methods like "i = i + 1" or "i++" for the loop iteration.
  • A different participant emphasizes the need to return boolean values instead of strings and recommends using the array method indexOf for checking existence.
  • There is a suggestion to declare variables properly within the loop for clarity and correctness.

Areas of Agreement / Disagreement

Participants provide differing suggestions on how to implement the loop and handle the return values, indicating that multiple approaches are being considered without a consensus on the best method.

Contextual Notes

There are unresolved issues regarding variable declarations and the specific implementation details of the loop, as well as the choice between different methods for checking array contents.

Who May Find This Useful

Individuals learning JavaScript, particularly those interested in array manipulation and loop structures.

mindauggas
Messages
127
Reaction score
0
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)
 
Technology news on Phys.org
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
 
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;
    }
}
mindauggas said:
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.
 
Thanks.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 8 ·
Replies
8
Views
6K
Replies
235
Views
15K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 14 ·
Replies
14
Views
4K