Can anyone explain this code.why is the 'd' used | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone explain this code.why is the 'd' used

const arr = ['0', '1', '4', 'a', '9', 'c', '16']; const my_obj = { [Symbol.iterator]: function*() { for(let index of arr) { yield `${index}`; } } }; const all = [...my_obj] .map(i => parseInt(i, 10)) .map(Math.sqrt) .filter((i) => i < 5) .reduce((i, d) => i + d); console.log(all);

23rd Jul 2020, 6:22 AM
Ursila Pradeep
Ursila Pradeep - avatar
2 Answers
+ 1
i don't know much about generators and iterators.. but the line const all = [...my_obj] it spreads the values of the array (the my_obj contains the content of the array), since generator yield the elements of the array.. then on that array, map high order function is used to convert each element to integer, so now the list is [0,1,4,NaN,9,NaN,16] then again using map, each element square root is done.. and then filter we get the elements which are less than 5 so the array is [0,1,2,3,4] and then comes the reduce function... so reduce takes two parameters you can name whatever you want.. first parameter is the accumulator(here "i") which has default value of 0 and the second parameter is the element of the array (here , "d") so, it add each element d to i.. where initially i=0 and after adding we get 10
23rd Jul 2020, 7:04 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar