-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34b5be5
commit 0abdc63
Showing
3 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); |