JavaScript arrays and loops combination question

In summary, the conversation discusses using the variable i in a for loop to incorporate array positions and returning boolean values instead of strings in the function isLost.
  • #1
127
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
  • #2
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
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.
 
  • #5


(i) Yes, it is possible to incorporate array positions into the for loop by using the variable i to represent the index of the array. For example, the for loop could be written as: for (i = 0; i < count; i++). This way, i will start at 0, which is the first index of the array, and increment by 1 until it reaches the length of the array.

(ii) To correctly incorporate array positions into the for loop, you can use the variable i as the index of the array within the loop. For example, instead of using lost[0] as the starting point, you can use lost to represent the first element of the array in each iteration of the loop. This way, the loop will go through each element of the array, starting from the first one, and check if it is equal to the input n.
 

1. What are JavaScript arrays?

JavaScript arrays are special variables that can hold multiple values in a single variable. They are a data structure used to store a collection of elements, such as numbers, strings, objects, or even other arrays.

2. How do you create an array in JavaScript?

To create an array in JavaScript, you can use the array literal notation [], or the array constructor new Array(). For example, var myArray = [1, 2, 3]; or var myArray = new Array(1, 2, 3);

3. What are loops in JavaScript?

Loops are used to iterate through an array or perform a task repeatedly until a certain condition is met. There are different types of loops in JavaScript, such as for loop, while loop, do-while loop, and for...in loop.

4. How do you access elements in an array?

You can access elements in an array by using their index number. The index starts at 0, so the first element in an array is at index 0, the second element is at index 1, and so on. For example, to access the third element in an array named myArray, you would use myArray[2].

5. How can arrays and loops be used together in JavaScript?

Arrays and loops can be used together to iterate through an array and perform a task on each element. For example, you can use a for loop to go through each element in an array and perform a calculation, or use a for...in loop to access the properties of an object stored in an array. You can also use loops to add or remove elements from an array, or to filter and manipulate the elements in an array.

Suggested for: JavaScript arrays and loops combination question

Replies
2
Views
937
Replies
4
Views
916
Replies
9
Views
939
Replies
11
Views
1K
Replies
1
Views
973
Replies
4
Views
1K
Replies
5
Views
1K
Replies
6
Views
880
Back
Top