Skip to content

Commit c024ea1

Browse files
committed
Complete drop it algorithm
1 parent d26d4e1 commit c024ea1

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function dropElements(arr, func) {
2+
const element = arr.find(func);
3+
if (element === undefined) {
4+
return [];
5+
}
6+
return arr.slice(arr.indexOf(element));
7+
}

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
1818
- [Sum All Odd Fibonacci Numbers](#sum-all-odd-ficonacci-numbers)
1919
- [Sum All Primes](#sum-all-primes)
2020
- [Smallest Common Multiple](#smallest-common-multiple)
21+
- [Drop It](#drop-it)
2122

2223
#### Sum All Numbers In a Range
2324

@@ -336,3 +337,19 @@ export function smallestCommons(arr) {
336337
}
337338
}
338339
```
340+
341+
#### Drop It
342+
343+
Given the array `arr`, iterate through and remove each element starting from the first element (the 0 index) until the function `func` returns `true` when the iterated element is passed through it.
344+
345+
Then return the rest of the array once the condition is satisfied, otherwise, arr should be returned as an empty array.
346+
347+
```javascript
348+
export function dropElements(arr, func) {
349+
const element = arr.find(func);
350+
if (element === undefined) {
351+
return [];
352+
}
353+
return arr.slice(arr.indexOf(element));
354+
}
355+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { dropElements } from "../../src/fcc-intermediate-algorithms/drop_it";
2+
3+
test('should remove elements that do not satisfy the function', () => {
4+
expect(dropElements([1, 2, 3, 4], (n) => n >= 3)).toEqual([3, 4]);
5+
expect(dropElements([1, 2, 3, 7, 4], (n) => n > 3)).toEqual([7, 4]);
6+
expect(dropElements([0, 1, 0, 1], (n) => n === 1)).toEqual([1, 0, 1]);
7+
expect(dropElements([1, 2, 3, 4], (n) => n > 5)).toEqual([]);
8+
});

0 commit comments

Comments
 (0)