-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
30 lines (23 loc) · 862 Bytes
/
map.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Do NOT use .map, to complete this function.
/*
How map works: Map calls a provided callback function once
for each element in an array, in order, and function constructs a new array from the res .
Produces a new array of values by mapping each value in list
through a transformation function (iteratee).
Return the new array.
*/
function map(elements, cb) {
let resultArr=[];
for(let index=0;index<elements.length;index++){
/*we will pass array's element, index in cb function so it will return some value
then we will store that value to new array (resultArr)
*/
let newValue=cb(elements[index],index);
resultArr.push(newValue);
}
/*after Iterating over a list of elements and storing in new Array we will
return that new Array ;
*/
return resultArr;
}
module.exports=map;