Rest parameters | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

Rest parameters

Hello co-sololearners, how this function is read? function containsAll(arr) { for (let k = 1; k < arguments.length; k++) { let num = arguments[k]; if (arr.indexOf(num) === -1) { return false; } } return true; }

4th May 2020, 4:23 PM
Unar Arbab
Unar Arbab - avatar
1 Respuesta
+ 5
Hello there Unar Arbab :)) Let's break down the code : Whenever you make a function in javascript, a property called 'argument' is made as well. Which is an array and contains all the arguments you pass in the function! Using a for loop you are iterating over the arguments. if (arr.indexOf(num) === -1) is checking if the argument exists in the array called 'arr'; Let's check it : console.log(containsAll([4,3], 3); // true Because the array is of 3 and 4. It has all the arguments we have passed. Now if you write it like so : console.log(containsAll([4,3], 5); // false Because the array isn't containing the argument we have passed! Hope this helps :))
4th May 2020, 4:39 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar