Skip to content

Commit e4144d1

Browse files
feat(curriculum): add lab for building a symmetric difference function (freeCodeCamp#61950)
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
1 parent c2ff82e commit e4144d1

5 files changed

Lines changed: 118 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
@@ -3486,6 +3486,12 @@
34863486
"In this lab, you will create a function that finds the index at which a given number should be inserted into a sorted array to maintain the array's sorted order."
34873487
]
34883488
},
3489+
"lab-symmetric-difference": {
3490+
"title": "Build a Symmetric Difference Function",
3491+
"intro": [
3492+
"In this lab, you will practice using higher order functions to find the symmetric difference between two arrays."
3493+
]
3494+
},
34893495
"review-javascript-higher-order-functions": {
34903496
"title": "JavaScript Higher Order Functions Review",
34913497
"intro": [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Build a Symmetric Difference Function
3+
block: lab-symmetric-difference
4+
superBlock: full-stack-developer
5+
---
6+
7+
## Introduction to the Symmetric Difference Function
8+
9+
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
id: 68ad9821ee41baad9cb0fd4e
3+
title: Build a Symmetric Difference Function
4+
challengeType: 25
5+
dashedName: lab-symmetric-difference
6+
---
7+
8+
# --description--
9+
10+
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
11+
12+
Example:
13+
14+
- Array A: `["diamond", "stick", "apple"]`
15+
16+
- Array B: `["stick", "emerald", "bread"]`
17+
18+
Result: `["diamond", "apple", "emerald", "bread"]`
19+
20+
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
21+
22+
**User Stories:**
23+
24+
1. Your function `diffArray` should return an array.
25+
2. Your function should take two arguments, both of which are arrays.
26+
3. Your function should make use of the `filter` method.
27+
4. Your function should return the symmetric difference of the two arrays.
28+
5. Your function should return an empty array if there is no symmetric difference.
29+
30+
31+
# --hints--
32+
33+
You should have a function named `diffArray`.
34+
35+
```js
36+
assert.isFunction(diffArray);
37+
```
38+
39+
The `diffArray` function should use the `filter` method to filter out items that are present in both arrays.
40+
41+
```js
42+
assert(/\.filter\(/.test(diffArray.toString()));
43+
```
44+
45+
`diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"])` should return `["pink wool"]`.
46+
47+
```js
48+
assert.deepEqual(diffArray(
49+
["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"],
50+
["diorite", "andesite", "grass", "dirt", "dead shrub"]
51+
), ["pink wool"]);
52+
```
53+
54+
`diffArray*["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"])` should return `["diorite", "pink wool"]`.
55+
56+
```js
57+
assert.deepEqual(diffArray(
58+
["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"],
59+
["andesite", "grass", "dirt", "dead shrub"]
60+
), ["diorite", "pink wool"]);
61+
```
62+
63+
`diffArray` should return an empty array when called with two identical arrays.
64+
65+
```js
66+
assert.deepEqual(diffArray(
67+
["andesite", "grass", "dirt", "dead shrub"],
68+
["andesite", "grass", "dirt", "dead shrub"]
69+
), []);
70+
```
71+
72+
# --seed--
73+
74+
## --seed-contents--
75+
76+
```js
77+
78+
```
79+
80+
# --solutions--
81+
82+
```js
83+
function diffArray(arr1, arr2) {
84+
return arr1
85+
.filter(item => !arr2.includes(item))
86+
.concat(arr2.filter(item => !arr1.includes(item)));
87+
}
88+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Build a Symmetric Difference Function",
3+
"isUpcomingChange": false,
4+
"dashedName": "lab-symmetric-difference",
5+
"helpCategory": "JavaScript",
6+
"challengeOrder": [
7+
{
8+
"id": "68ad9821ee41baad9cb0fd4e",
9+
"title": "Build a Symmetric Difference Function"
10+
}
11+
],
12+
"blockLayout": "link",
13+
"blockType": "lab"
14+
}

curriculum/structure/superblocks/full-stack-developer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@
395395
"workshop-library-manager",
396396
"lab-book-organizer",
397397
"lab-sorted-index-finder",
398+
"lab-symmetric-difference",
398399
"review-javascript-higher-order-functions",
399400
"quiz-javascript-higher-order-functions"
400401
]

0 commit comments

Comments
 (0)