Skip to content

Commit d69deaa

Browse files
authored
Create Problem13.js
groups elements in an array based on a given condition.
1 parent adb1206 commit d69deaa

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

problems/Problem13.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Implement a function that groups elements in an array based on a given condition.
2+
//For example, grouping even and odd numbers into separate arrays.
3+
4+
function groupByCondition(arr,condition){
5+
return [
6+
arr.filter(e => condition(e)),
7+
arr.filter(e => !condition(e))
8+
];
9+
}
10+
11+
const arr = [ 2 , 5 , 1 , 9 , 3 , 7 , 8 ];
12+
let isEven = num => num%2===0;
13+
const finalArr = groupByCondition(arr,isEven);
14+
console.log(finalArr);

0 commit comments

Comments
 (0)