简体   繁体   中英

understanding square brackets in for loop- lesson

I'm currently learning Javascript and would love if someone could help me understand the for loops further. I want to see if someone can give me a bit of an in depth explanation as to how this loop works.

The idea is to return the first non consecutive number in the argument, which as you can see is 6.

Because I'm still learning I wanted to get a detailed yet easy understanding of how this works for example, what's the difference between arr[i]+1 and arr[i+1] ?

function firstNonConsec(arr){
    for(let i = 0; i < arr.length - 1; i++){
        if(arr[i] + 1 !== arr[i+1]){
            return arr[i + 1];
        }
    }
    return null
};


console.log(firstNonConsec([1,2,3,4,6,7,8]));

what's the difference between arr[i]+1 and arr[i+1] ?

This is not a question about the for loop, but about arrays.

if arr is an array, then you can get the value of one of its items by doing arr[item_number]

arr[i]+1 will therefore give you the value at the place i of the table (eg if i equals 0, that would be the first entry in the array), plus one* arr[i+1] will give you the value at the place i+1 of the table (eg if i equals 0, that would be the second entry in the array)

  • note that +1 can do a lot of things in Javascript, depending on type auto conversion; in your case with only numbers it will increase the number by 1

You can get reference from this workflow of firstNonConsec([1,2,3,4,6,7,8])

            i :    0  1  2  3  4  5  6  

       arr[i] :  [ 1, 2, 3, 4, 6, 7, 8 ]
                
   arr[i] + 1 :    2  3  4  5  7  8  9

     arr[i+1] :    2  3  4  6  7  8  N
 
 if-statement :    T  T  T  F  T  T  F           // T: true F: false

       return :    2  3  4  N  7  8  N           // N: null

for loop is basically a special type of while loop

var i = 0;
while (i < 10) {
    // < code >
    i++
}

is the same as

for (let i = 0; i < 10; i++) {
    //< code >
}

while loops are used for many different things, so remember them, but for loops are used most. Basically the first "segment" (I'm gonna call whatever's before a semicolon a segment) runs before whatever code you wrote. It usually declares a variable. The second segment runs every time, and stops the loop if the condition isn't met. The third segment also runs every time, but it just runs some code after all the code you wrote is written.. In a lot of languages, including JavaScript, you can also loop through arrays and HashMaps. In JavaScript, you use in and of words. use for/in for objects

for (let x in < object >) {
    //< code >
}

and for each iteration, x is the key of one of the object's properties

to loop through arrays and other iterable objects (you can use for/in, but it's bad practice to), use for/of

for (var x of < array >) {
    //< code >
}

this loops through the values of an array, or other such iterable object

the difference between arr[i+1] and arr[i]+1 is that arr[i+1] will access the element after the index specified, but arr[i]+1 will take the value of the index, and return that + 1 (won't change the value, use += to change value). BTW you don't always have to use of for looping thru arrays, you can do it this way which takes i and increases it every time, then takes the value of the index of i.

the answer to your problem:

function firstNonConsec(arr) {
    let temp = arr[0];
    for (let i = 1; i < arr.length; i++) {
        temp = arr[i - 1];
        if (temp !== arr[i] - 1) return arr[i];
    }
    return null;
}

I'm not using for of because you didn't, but comment if you want me to write with for of Note I used let most of the time because you don't want there to be a random variable that you don't need, and let is limited to the scope of the loop. Also, iterating variables are commonly named i, j, x, y, z and such so you might use i a lot.

arr[i]+1 returns the value of arr's i th array position, then adds one to that value.

arr = [2, 9, 5, 1]
i = 2
arr[2] + 1


Result: 6

So it 1) finds the item in that position 2) adds 1. An array's index starts at 0: arr[n] = [0+n]

You always get the value equal to the result within the bracket.

arr[i+1] would return the i+1th value.

arr = [2, 9, 5, 1]
i = 2
print(arr[i+1]) == print(arr[3])

Result: 1

So this one changes the position itself by 1. This is how a robocaller might complete a call then select the next phone number in a list.

In your case:

 if(arr[i] + 1 !== arr[i+1]){
        return arr[i + 1];

if the value of the next item in the list is not equal to the previous value + 1, return that value. The function prints you a list of every non consecutive number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM