0

Javascript rest parameters

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));//true console.log(containsAll(x, 6, 4, 9));//false why does first array becomes true while the other one is false since they have the nmber arguements?

28th Mar 2019, 5:38 PM
Mas Hairul Adham
Mas Hairul Adham  - avatar
4 ответов
+ 2
The function searches all numbes in the array. In the first statement all numbers are found, and true is returned. In the second statement the 9 is not found in the array, so false is returned.
28th Mar 2019, 6:53 PM
PapaBT
PapaBT - avatar
+ 1
so why did the x is in within the ()
28th Mar 2019, 6:58 PM
Mas Hairul Adham
Mas Hairul Adham  - avatar
0
Its a parameter for the function. Its assignt to arr. The other argumeters are accessed via the arguments array.
28th Mar 2019, 7:12 PM
PapaBT
PapaBT - avatar
0
oh thank you very much
28th Mar 2019, 7:14 PM
Mas Hairul Adham
Mas Hairul Adham  - avatar