mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add Build an All-True Property Validator lab (#62198)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Sem Bauke <sem@freecodecamp.org>
This commit is contained in:
@@ -3576,6 +3576,12 @@
|
||||
"In this lab you will create a function that can flatten deeply nested arrays, handling any level of nesting without using built-in flat methods."
|
||||
]
|
||||
},
|
||||
"lab-all-true-property-validator": {
|
||||
"title": "Build an All-True Property Validator",
|
||||
"intro": [
|
||||
"In this lab you will build a function that checks if all objects in an array have a truthy value for a specific property."
|
||||
]
|
||||
},
|
||||
"review-javascript-higher-order-functions": {
|
||||
"title": "JavaScript Higher Order Functions Review",
|
||||
"intro": [
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build an All-True Property Validator
|
||||
block: lab-all-true-property-validator
|
||||
superBlock: full-stack-developer
|
||||
---
|
||||
|
||||
## Introduction to the Build an All-True Property Validator
|
||||
|
||||
For this lab, you will create a function that validates whether all objects in an array have a truthy value for a specific property.
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
---
|
||||
id: a10d2431ad0c6a099a4b8b52
|
||||
title: Build an All-True Property Validator
|
||||
challengeType: 26
|
||||
dashedName: build-an-all-true-property-validator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab you will test a specific property of each object in an array to see if it always has a truthy value or not.
|
||||
|
||||
For example, you could be asked to test one property of the objects in an array like the following:
|
||||
|
||||
```js
|
||||
[{
|
||||
name: "Quincy",
|
||||
role: "Founder",
|
||||
isBot: false
|
||||
}, {
|
||||
name: "Naomi",
|
||||
role: "",
|
||||
isBot: false
|
||||
}, {
|
||||
name: "Camperbot",
|
||||
role: "Bot",
|
||||
isBot: true
|
||||
}]
|
||||
```
|
||||
|
||||
If you were asked to test the `name` property, in the objects of this array the property `name` has the values of `"Quincy"`, `"Naomi"`, and `"Camperbot"`, so it is always truthy.
|
||||
|
||||
If you were asked to test the `role` property, the values are `"Founder"`, `""`, and `"Bot"`, in this case `""` is a falsy value, so the values are not always truthy.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should have a function named `truthCheck`.
|
||||
1. The `truthCheck` function takes two arguments: an array of objects and a string representing a property name found in those objects.
|
||||
1. The function should check if the property with the name equal to the second argument has a truthy value in all the objects of the array, and return `true` if it has, and `false` otherwise.
|
||||
|
||||
# --hints--
|
||||
|
||||
`truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "isBot")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", role: "Founder", isBot: false },
|
||||
{ name: "Naomi", role: "", isBot: false },
|
||||
{ name: "Camperbot", role: "Bot", isBot: true }
|
||||
],
|
||||
"isBot"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "name")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", role: "Founder", isBot: false },
|
||||
{ name: "Naomi", role: "", isBot: false },
|
||||
{ name: "Camperbot", role: "Bot", isBot: true }
|
||||
],
|
||||
"name"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "role")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", role: "Founder", isBot: false },
|
||||
{ name: "Naomi", role: "", isBot: false },
|
||||
{ name: "Camperbot", role: "Bot", isBot: true }
|
||||
],
|
||||
"role"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Pikachu", number: 25, caught: 3}, {name: "Togepi", number: 175, caught: 1}], "number")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Pikachu", number: 25, caught: 3 },
|
||||
{ name: "Togepi", number: 175, caught: 1 },
|
||||
],
|
||||
"number"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Pikachu", number: 25, caught: 3}, {name: "Togepi", number: 175, caught: 1}, {name: "MissingNo", number: NaN, caught: 0}], "caught")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Pikachu", number: 25, caught: 3 },
|
||||
{ name: "Togepi", number: 175, caught: 1 },
|
||||
{ name: "MissingNo", number: NaN, caught: 0 },
|
||||
],
|
||||
"caught"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Pikachu", number: 25, caught: 3}, {name: "Togepi", number: 175, caught: 1}, {name: "MissingNo", number: NaN, caught: 0}], "number")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Pikachu", number: 25, caught: 3 },
|
||||
{ name: "Togepi", number: 175, caught: 1 },
|
||||
{ name: "MissingNo", number: NaN, caught: 0 },
|
||||
],
|
||||
"number"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "Quincy", username: "QuincyLarson"}, {name: "Naomi", username: "nhcarrigan"}, {name: "Camperbot"}], "username")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "Quincy", username: "QuincyLarson" },
|
||||
{ name: "Naomi", username: "nhcarrigan" },
|
||||
{ name: "Camperbot" }
|
||||
],
|
||||
"username"), false);
|
||||
```
|
||||
|
||||
`truthCheck([{name: "freeCodeCamp", users: [{name: "Quincy"}, {name: "Naomi"}]}, {name: "Code Radio", users: [{name: "Camperbot"}]}, {name: "", users: []}], "users")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ name: "freeCodeCamp", users: [{ name: "Quincy" }, { name: "Naomi" }] },
|
||||
{ name: "Code Radio", users: [{ name: "Camperbot" }] },
|
||||
{ name: "", users: [] },
|
||||
],
|
||||
"users"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{id: 1, data: {url: "https://freecodecamp.org", name: "freeCodeCamp"}}, {id: 2, data: {url: "https://coderadio.freecodecamp.org/", name: "CodeRadio"}}, {id: null, data: {}}], "data")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ id: 1, data: { url: "https://freecodecamp.org", name: "freeCodeCamp" } },
|
||||
{ id: 2, data: { url: "https://coderadio.freecodecamp.org/", name: "CodeRadio" } },
|
||||
{ id: null, data: {} },
|
||||
],
|
||||
"data"), true);
|
||||
```
|
||||
|
||||
`truthCheck([{id: 1, data: {url: "https://freecodecamp.org", name: "freeCodeCamp"}}, {id: 2, data: {url: "https://coderadio.freecodecamp.org/", name: "CodeRadio"}}, {id: null, data: {}}], "id")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(truthCheck(
|
||||
[
|
||||
{ id: 1, data: { url: "https://freecodecamp.org", name: "freeCodeCamp" } },
|
||||
{ id: 2, data: { url: "https://coderadio.freecodecamp.org/", name: "CodeRadio" } },
|
||||
{ id: null, data: {} },
|
||||
],
|
||||
"id"), false);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function truthCheck(collection, pre) {
|
||||
return pre;
|
||||
}
|
||||
|
||||
truthCheck([{name: "Quincy", role: "Founder", isBot: false}, {name: "Naomi", role: "", isBot: false}, {name: "Camperbot", role: "Bot", isBot: true}], "isBot");
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function truthCheck(collection, pre) {
|
||||
return collection.every(function(e) { return e[pre]; });
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Build an All-True Property Validator",
|
||||
"blockType": "lab",
|
||||
"blockLayout": "link",
|
||||
"isUpcomingChange": false,
|
||||
"usesMultifileEditor": true,
|
||||
"dashedName": "lab-all-true-property-validator",
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "a10d2431ad0c6a099a4b8b52",
|
||||
"title": "Build an All-True Property Validator"
|
||||
}
|
||||
],
|
||||
"helpCategory": "JavaScript"
|
||||
}
|
||||
@@ -410,6 +410,7 @@
|
||||
"lab-matching-object-filter",
|
||||
"lab-sum-all-primes-calculator",
|
||||
"lab-deep-flattening-tool",
|
||||
"lab-all-true-property-validator",
|
||||
"review-javascript-higher-order-functions",
|
||||
"quiz-javascript-higher-order-functions"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user