+ 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?
7 Antworten
+ 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.
+ 1
Christine Jump please share your code first and specify what language you are asking about, thanks! :)
+ 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));
+ 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! :)
+ 1
Aha! Thank you. Yes that helps!
+ 1
Some light reading and visuals to help.
https://code.sololearn.com/Wa16a15a3a8A
https://www.w3schools.com/js/js_function_parameters.asp
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?