Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 382e72a

Browse files
committed
finders keepers 🔎
1 parent 235a6c4 commit 382e72a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
1919
- [6. Confirm the Ending Passed](#6-confirm-the-ending-passed)
2020
- [7. Repeat a String Repeat a String](#7-repeat-a-string-repeat-a-string)
2121
- [8. Truncate a String](#8-truncate-a-string)
22+
- [9. Finders Keepers](#9-finders-keepers)
2223

2324
## Basic Algorithm Scripting
2425

@@ -284,3 +285,36 @@ function truncateString(str, num) {
284285

285286
truncateString("A-tisket a-tasket A green and yellow basket", 8);
286287
```
288+
289+
### 9. Finders Keepers
290+
291+
_Difficulty: Beginner_
292+
293+
Create a function that looks through an array arr and returns the first element in it that passes a 'truth test'. This means that given an element x, the 'truth test' is passed if func(x) is true. If no element passes the test, return undefined.
294+
295+
---
296+
297+
#### Solution 1
298+
299+
```js
300+
function findElement(arr, func) {
301+
let num = 0;
302+
303+
for (let i = 0; i < arr.length; i++) {
304+
num = arr[i];
305+
if (func(num)) {
306+
return num;
307+
}
308+
}
309+
310+
return undefined;
311+
}
312+
```
313+
314+
#### Solution 2
315+
316+
```js
317+
function findElement(arr, func) {
318+
return arr.find(func);
319+
}
320+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /