In the above page,
let arr = [1, 2, 3, 4];
arr.every((elem, index, arr) => {
arr[index+1]--;
console.log(`[${arr}][${index}] -> ${elem}`);
return elem < 2;
})
In the above code, what does arr[index+1]–; mean?
Can any one plz explain.
In the above page,
let arr = [1, 2, 3, 4];
arr.every((elem, index, arr) => {
arr[index+1]--;
console.log(`[${arr}][${index}] -> ${elem}`);
return elem < 2;
})
In the above code, what does arr[index+1]–; mean?
Can any one plz explain.
This line decreases the next value in the array by one. For example in the first round the index
is 0
, arr[index+1]
is arr[1]
is 2
and arr[index+1]--
decreases the 2
to 1
. You can see the changed value in the array in the first output of the example: // 1st iteration: [1,1,3,4][0] -> 1
Does that make it clear?
Michael
Yes, Sorry for such a stupid question
And thank u @mikoMK for your patience