+ 1
Hi everybody, can someone explain me ?
let car=[13000, 23000, 100, 10]; let [price, tag, ...rest]=car; let out=price+tag; console.log(out/car.indexOf(...rest)); //outputs -36000 But I don't know how does car.indexOf(...rest) give me -1. Need explaination please
3 Answers
+ 2
You should know that general syntax of indexOf is,
Array.indexOf(elem, [startIdx])
So, car.indexOf(...rest) resolves to,
>>> [13000, 23000, 100, 10].indexOf(100, 10)
which means js finds 100 starting from 10th index, i.e., 11th element!
Now, you can think why result is -1
;-)
0
car.indexOf(rest[0])
Edit: you are searching an array with rest. car doesn't contain the array rest, it contains the values of that array. that's why it returns -1 because it didn't find a match.
0
Okay @vrintle & QTWizard thank y'all for your help. Now I see where was my mistake.
Helpful