Skip to content

Commit

Permalink
created mapFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
Lovegupta112 committed Aug 29, 2023
1 parent 34b5be5 commit 0abdc63
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
30 changes: 30 additions & 0 deletions map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function each(elements, cb) {
```
function map(elements, cb) {
// 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 functionructs a new array from the res .
// 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.
}
Expand Down
11 changes: 11 additions & 0 deletions test/testMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mapFunc=require('../map');

const items = [1, 2, 3, 4, 5, 5];

function squareFunc(element){
return element*element;
}

let squares=mapFunc(items,squareFunc);

console.log(`square of ${items} : ${squares}`);

0 comments on commit 0abdc63

Please sign in to comment.