Mode entretien. Minuteur de 5 min, sans indices ni autocomplétion
Your own map
FacileFunctions5 min
Write a function map(arr, fn) without the built-in Array.prototype.map.
It returns a new array where each element is replaced by fn(arr[i], i). The callback fn gets the element and its index. Do not change the input array.
Exemples
map([1, 2, 3], (n) => n + 1)Renvoie [ 2, 3, 4 ]
map([10, 20, 30], (n, i) => n * i)Renvoie [ 0, 20, 60 ]
Chargement de l'éditeur...
- Adds 1 to each element
- Passes the index as the second argument
- An empty array gives an empty array
Les tests cachés se lancent à la soumission