Skip to content

Commit 64b32cf

Browse files
feat: add finders keepers algorithm to full stack (freeCodeCamp#61332)
Co-authored-by: Zaira <33151350+zairahira@users.noreply.github.com>
1 parent 35ce565 commit 64b32cf

5 files changed

Lines changed: 159 additions & 0 deletions

File tree

client/i18n/locales/english/intro.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3280,6 +3280,12 @@
32803280
"In this lab, you will use JavaScript fundamentals to create a function that finds the largest number in each sub-array of a given array."
32813281
]
32823282
},
3283+
"lab-first-element-finder": {
3284+
"title": "Build a First Element Finder",
3285+
"intro": [
3286+
"In this lab, you will create a function that looks through an array and returns the first element in it that passes a \"truth test\"."
3287+
]
3288+
},
32833289
"lab-slice-and-splice": {
32843290
"title": "Implement the Slice and Splice Algorithm",
32853291
"intro": [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Introduction to the Build a First Element Finder
3+
block: lab-first-element-finder
4+
superBlock: full-stack-developer
5+
---
6+
7+
## Introduction to the Build a First Element Finder
8+
9+
In this lab, you will create a function that looks through an array and returns the first element in it that passes a "truth test".
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "Build a First Element Finder",
3+
"blockType": "lab",
4+
"blockLayout": "link",
5+
"isUpcomingChange": false,
6+
"dashedName": "lab-first-element-finder",
7+
"superBlock": "full-stack-developer",
8+
"helpCategory": "JavaScript",
9+
"challengeOrder": [{ "id": "a6e40f1041b06c996f7b2406", "title": "Build a First Element Finder" }],
10+
"usesMultifileEditor": true
11+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
id: a6e40f1041b06c996f7b2406
3+
title: Build a First Element Finder
4+
challengeType: 26
5+
dashedName: build-a-first-element-finder
6+
---
7+
8+
# --description--
9+
10+
In this lab, you will create a function that looks through an array and returns the first element that passes a test function (also known as a "truth test").
11+
12+
The function would iterate through the array and test each element using the provided test function. At the end, it would return the first element where the test function returns `true`.
13+
14+
Example:
15+
16+
```js
17+
findElement([1, 3, 5, 8], num => num % 2 === 0) // returns 8
18+
findElement([1, 3, 5], num => num % 2 === 0) // returns undefined
19+
```
20+
21+
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
22+
23+
**User Stories:**
24+
25+
1. You should have a function named `findElement` that accepts an array and a function as arguments.
26+
2. The function should return the first item in the array that passes a truth test. This means that, calling the passed in function `func`, given an element `x`, the truth test is passed if `func(x)` is `true`.
27+
3. If no element passes the test, the function should return `undefined`.
28+
29+
# --hints--
30+
31+
You should have a `findElement` function.
32+
33+
```js
34+
assert.isFunction(findElement);
35+
```
36+
37+
`findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; })` should return `8`.
38+
39+
```js
40+
assert.strictEqual(
41+
findElement([1, 3, 5, 8, 9, 10], function (num) {
42+
return num % 2 === 0;
43+
}),
44+
8
45+
);
46+
```
47+
48+
`findElement([1, 3, 5, 9], function(num) { return num % 2 === 0; })` should return `undefined`.
49+
50+
```js
51+
assert.strictEqual(
52+
findElement([1, 3, 5, 9], function (num) {
53+
return num % 2 === 0;
54+
}),
55+
undefined
56+
);
57+
```
58+
59+
`findElement([1, 2, 3, 4], function(num) { return num > 2; })` should return `3`.
60+
61+
```js
62+
assert.strictEqual(
63+
findElement([1, 2, 3, 4], function (num) {
64+
return num > 2;
65+
}),
66+
3
67+
);
68+
```
69+
70+
`findElement(["hello", "world", "javascript"], function(str) { return str.length > 5; })` should return `"javascript"`.
71+
72+
```js
73+
assert.strictEqual(
74+
findElement(["hello", "world", "javascript"], function (str) {
75+
return str.length > 5;
76+
}),
77+
"javascript"
78+
);
79+
```
80+
81+
`findElement(["cat", "dog", "bird"], function(str) { return str.length > 10; })` should return `undefined`.
82+
83+
```js
84+
assert.strictEqual(
85+
findElement(["cat", "dog", "bird"], function (str) {
86+
return str.length > 10;
87+
}),
88+
undefined
89+
);
90+
```
91+
92+
`findElement([2, 4, 6, 8], function(num) { return num % 2 === 0; })` should return `2`.
93+
94+
```js
95+
assert.strictEqual(
96+
findElement([2, 4, 6, 8], function (num) {
97+
return num % 2 === 0;
98+
}),
99+
2
100+
);
101+
```
102+
103+
`findElement([], function(num) { return num > 0; })` should return `undefined`.
104+
105+
```js
106+
assert.strictEqual(
107+
findElement([], function (num) {
108+
return num > 0;
109+
}),
110+
undefined
111+
);
112+
```
113+
114+
# --seed--
115+
116+
## --seed-contents--
117+
118+
```js
119+
120+
```
121+
122+
# --solutions--
123+
124+
```js
125+
function findElement(arr, func) {
126+
return arr.filter(func)[0];
127+
}
128+
129+
findElement([1, 2, 3, 4], num => num % 2 === 0);
130+
```

curriculum/superblock-structure/full-stack.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,9 @@
731731
{
732732
"dashedName": "lab-largest-number-finder"
733733
},
734+
{
735+
"dashedName": "lab-first-element-finder"
736+
},
734737
{
735738
"dashedName": "lab-slice-and-splice"
736739
},

0 commit comments

Comments
 (0)