Skip to content

Commit effd1d3

Browse files
committed
Complete everything be true algorithm
1 parent 2af5628 commit effd1d3

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function truthCheck(collection, pre) {
2+
return collection.every((val) => Boolean(val[pre]));
3+
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
2020
- [Smallest Common Multiple](#smallest-common-multiple)
2121
- [Drop It](#drop-it)
2222
- [Binary Agents](#binary-agents)
23+
- [Everything Be True](#everything-be-true)
2324

2425
#### Sum All Numbers In a Range
2526

@@ -380,3 +381,19 @@ export function binaryAgents(str) {
380381
return stringArray.join("");
381382
}
382383
```
384+
385+
#### Everything Be True
386+
387+
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
388+
389+
In other words, you are given an array collection of objects. The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false.
390+
391+
In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context.
392+
393+
Remember, you can access object properties through either dot notation or [] notation.
394+
395+
```javascript
396+
export function truthCheck(collection, pre) {
397+
return collection.every((val) => Boolean(val[pre]));
398+
}
399+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { truthCheck } from "../../src/fcc-intermediate-algorithms/everything_be_true";
2+
3+
test('should ', () => {
4+
expect(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")).toBe(true);
5+
expect(truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")).toBe(false)
6+
});

0 commit comments

Comments
 (0)