Skip to content

Commit

Permalink
created filter Function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lovegupta112 committed Aug 29, 2023
1 parent c27c0df commit 97c9280
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Do NOT use .filter, to complete this function.
// Similar to `find` but you will return an array of all elements that passed the truth test
// Return an empty array if no elements pass the truth test

function filter(elements, cb) {

let targetElementsArr=[];

for(let index=0;index<elements.length;index++){
if(cb(elements[index])){
targetElementsArr.push(elements[index]);
}
}
return targetElementsArr;
}

module.exports=filter;
17 changes: 17 additions & 0 deletions test/testFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

let filterFunc=require('../filter');

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

function isEvenNumber(element){
if(element%2==0){
return true;
}
else {
return false;
}
}

let evenNumbers=filterFunc(items,isEvenNumber);

console.log("Even Numbers :",evenNumbers);

0 comments on commit 97c9280

Please sign in to comment.