Could someone please explain this, and Rest and spread parameters better? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Could someone please explain this, and Rest and spread parameters better?

function magic(...nums) { let sum = 0; nums.filter(n => n % 2 == 0).map(el => sum+= el); return sum; } console.log(magic(1, 2, 3, 4, 5, 6)); This is REALLY confusing to me, help would be GREATLY appriciated. Thank you

14th Jul 2019, 6:01 PM
Bryan
Bryan - avatar
3 Answers
+ 12
------------------------------- function magic(...nums){ } ------------------------------- This is a rest parameter, and it is used to tell JS that this function has an unknown number of arguments. This is quite useful when working with dynamic data. You don't have a fixed number of arguments, and when you call the function, you are allowed to pass as many arguments as you want. The arguments that are placed into the function-call is then converted into an array. The "nums" variable is simply the name that the array of numbers is given when the function is called. ------------------------------------------------------------ nums.filter(n => n % 2 == 0).map(el => sum+= el); ------------------------------------------------------------- The filter function filters numbers where the modulus is 0, meaning that the number divided by "2" gives 0 remainders. Example: 2 % 2 = 0 (because 2 divided by 2 is 0), 4 % 2 = 0 (because 4 divided by 2 is 2, and 2 divided by 2 is, again, 0). The map function then has an array of numbers which are dividable by 2, and then adds all the numbers together. Your starting array: [1, 2, 3, 4, 5, 6] After filter: [2, 4, 6] 2 + 4 + 6 = 12 Hope this helped! Feel free to message me if you have any further questions :)
14th Jul 2019, 6:33 PM
Roolin
Roolin - avatar
+ 5
All you have to do is to first remove the %2 =0 passes into the filter method. Let's try breaking it The argument ...nums in the magics are 1,2,3,4,5,6, If we filter the nums aguments with %2= 0; 1%2 != 0, 2%2 = 0, 3%2 != 0, 4%2 = 0, 5%2 != 0, 6%2 = 0 Now from the filters above Only 2 ,4,6 passed the test. From the test passed item, we attached another method map: which says to add the whole item that passed the test to variable sum Now sum = 2+4+6, Which means sum = 12 So our answer is 12.. --credit to igwe Big thanks to him! and big thanks to Roolin!
14th Jul 2019, 6:20 PM
Bryan
Bryan - avatar
+ 1
Bryan filter()??? map()???? function filter(n => n%2==0) try to check all nums for values that will give remainder 0 if divided by 2 filter(n => n %2 == 0) checks the objects "n" in nums with modulus operation and add the object with true result "el" in sum while discarding any false results, function map(el => sum +=el) works here to add up the filtered nums i.e 2,4,6 in simple terms here we go; var nums = [1,2,3,4,5,6], n = nums, sum = 0, el = 0; //function filter(n => n%2==0) for(let i=0; i<n.length; i++){ if (n[i]%2==0) { //function map(el => sum +=el) el = n[i]; // 2, 4, 6 sum += el; //2+4+6 } } console.log(sum); //12
18th Aug 2019, 12:29 AM
Al-Amin Rais
Al-Amin Rais - avatar