Hi everybody, can someone explain me ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

24th Oct 2020, 12:26 PM
Jerry Bisaliko
Jerry Bisaliko - avatar
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 ;-)
24th Oct 2020, 1:02 PM
777
777 - avatar
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.
24th Oct 2020, 12:40 PM
QTWizard
0
Okay @vrintle & QTWizard thank y'all for your help. Now I see where was my mistake. Helpful
24th Oct 2020, 1:19 PM
Jerry Bisaliko
Jerry Bisaliko - avatar