[k] set to 1 but accesses [0]? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[k] set to 1 but accesses [0]?

Shouldn't K be set to 0 in the for loop? How is it finding the first argument in the array when set to 1?

27th Feb 2021, 2:17 AM
Christine Jump
Christine Jump - avatar
7 Answers
+ 2
Yes the first property of the Arguments Object is the array that is to be checked. The arguments themselves are a property of the Arguments Object, which is array-like in that it is iterable, it has a length property but it can not access any of the Array methods such as slice(), map(), or findIndexOf(). Yes you are getting closer😊 The Array method findIndexOf() will return either the index if any of the elements in the arr that match the iterated properties of the Arguments object, otherwise it returns -1.
27th Feb 2021, 5:31 AM
ODLNT
ODLNT - avatar
+ 1
Christine Jump please share your code first and specify what language you are asking about, thanks! :)
27th Feb 2021, 2:21 AM
TheCoder
+ 1
Right. It's JavaScript. Here is the example code I'm referring to: function containsAll(arr) { for (let k = 1; k < arguments.length; k++) { let num = arguments[k]; if (arr.indexOf(num) === -1) { return false; } } return true; } let x = [2, 4, 6, 7]; console.log(containsAll(x, 2, 4, 7)); console.log(containsAll(x, 6, 4, 9));
27th Feb 2021, 2:26 AM
Christine Jump
Christine Jump - avatar
+ 1
Christine Jump you put the array x and 3 numbers to the function right? Well... I tried to document.write all the numbers that it accessed through arguments[k] and it actually doesn't access the array x! It only accesses the 3 numbers! It accesses array x when you use the arr keyword though, because x is an array and the rest are arguments! Hope it helps! Happy programming and keep learning! :)
27th Feb 2021, 2:38 AM
TheCoder
+ 1
Aha! Thank you. Yes that helps!
27th Feb 2021, 3:29 AM
Christine Jump
Christine Jump - avatar
27th Feb 2021, 3:48 AM
ODLNT
ODLNT - avatar
0
So, the first argument is the array we're checking. The arguments themselves are an array object. I'm getting closer. Does the -1 and 1 of the var num reflect a boolean condition, returned in the indexOf method performed on the arr of arguments?
27th Feb 2021, 3:57 AM
Christine Jump
Christine Jump - avatar