Skip to content

Commit

Permalink
created findFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
Lovegupta112 committed Aug 29, 2023
1 parent b4c4eaf commit c27c0df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
25 changes: 25 additions & 0 deletions find.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Do NOT use .includes, to complete this function.
// Look through each value in `elements` and pass each element to `cb`.
// If `cb` returns `true` then return that element.
// Return `undefined` if no elements pass the truth test.


function find(elements, cb) {

let targetElement;

for(let index=0;index<elements.length;index++){

if(cb(elements[index])){
targetElement=elements[index];
return targetElement;
}
}

return targetElement;

}

module.exports=find;


13 changes: 13 additions & 0 deletions test/testFind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const findFunc=require('../find');

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

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

const evenNumber=findFunc(items,isEvenNumber);

console.log("Even Number :",evenNumber);

0 comments on commit c27c0df

Please sign in to comment.