How to do a function that accepts an array AND a callback function as parameters

Hey everyone,

I have been given this optional exercise for my school, that dives into advanced functions.
The exercise has 3 parts, and I did the first without diffulties. But then…

I tried to do a function as asked, but can’t achieve to get the right result.

Here is a link to my stackblitz : ** Exercice - Fonctions avancées - StackBlitz **

I’m not asking for the solution, just for help to get unstuck !
Thanks in advance for your time,

François

A few questions:

  • Why are using the spread syntax (...people) to pass in an argument to your function? Your arrayRefacto function expects an array as the first argument.

  • Have a look at the documentation for forEach and map. In particular, look at the “Return value” section in the documentation. Which of the two methods is more suitable for your task?

1 Like

Thanks mtrtmk for your implication,

  • I was using the spread syntax because it wasn’t clear for me how I could use an array as a parameter, but it seems that it’s like anything else !

  • I went to see the documentation for forEach and map.
    I understand that the difference between those 2 is :

    • forEach returns undefined
    • map returns a new array with the result of the callback function

I know .map would be amazing but I simply can’t use it in the exercise, I need to modify the original array !

It seems that I missed some pieces on my way to the solution, as I thought not necessary to take the index as a parameter of the forEach function, to be able to call the callback function on each element of the array.