Modo entrevista. Cronômetro de 5 min, sem dicas e sem autocompletar
Your own map
FácilFunctions5 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.
Exemplos
map([1, 2, 3], (n) => n + 1)Retorna [ 2, 3, 4 ]
map([10, 20, 30], (n, i) => n * i)Retorna [ 0, 20, 60 ]
Carregando o editor...
- Adds 1 to each element
- Passes the index as the second argument
- An empty array gives an empty array
Os testes ocultos rodam no envio