Skip to content

Commit b972e3e

Browse files
authored
Rewrite "array destructuring" exercises as object destructuring (#47)
These exercises do not actually benefit from array destructuring, so are quite confusing as-is. But they are great examples of object destructuring. Let's call them that. Also move them to a Sprint directiry, like all of the other modules are laid out.
1 parent 4fbe2bd commit b972e3e

8 files changed

Lines changed: 50 additions & 37 deletions

File tree

fetch/array-destructuring/exercise-1/exercise.js renamed to Sprint-1/destructuring/exercise-1/exercise.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const personOne = {
44
favouriteFood: "Spinach",
55
};
66

7+
// Update the parameter to this function to make it work.
8+
// Don't change anything else.
79
function introduceYourself(___________________________) {
810
console.log(
911
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
We can use object destructuring to extract values from an object and assign them to variables in one line.
2+
3+
```js
4+
let person = {
5+
firstName: "Bruce",
6+
lastName: "Wayne",
7+
};
8+
9+
let { firstName, lastName } = person;
10+
11+
console.log(`Batman is ${firstName}, ${lastName}`);
12+
```
13+
14+
The program above will print `Batman is Bruce Wayne`.
15+
16+
This is more concise than doing this without object destructuring:
17+
18+
```js
19+
let person = {
20+
firstName: "Bruce",
21+
lastName: "Wayne",
22+
};
23+
24+
let firstName = person.firstName;
25+
let lastName = person.lastName;
26+
27+
console.log(`Batman is ${firstName}, ${lastName}`);
28+
```
29+
30+
# Exercise
31+
32+
- What is the syntax to destructure the object `personOne` in exercise.js?
33+
- Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in.
File renamed without changes.

fetch/array-destructuring/exercise-2/readme.md renamed to Sprint-1/destructuring/exercise-2/readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# Exercise 2
22

3-
_Need some help? Refresh your memory with [this article](https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/)_
4-
53
In `exercise.js`, you have an array that contains a list of people who are at Hogwarts School of Witchcraft and Wizardry.
4+
65
For each character you have the following information:
76

87
- First Name
@@ -14,7 +13,7 @@ For each character you have the following information:
1413
## Task 1
1514

1615
- In `exercise.js` write a program that will take the `hogwarts` array as input and display the names of the people who belong to the Gryffindor house.
17-
- Use array destructuring to extract the values you need out of the array.
16+
- Use object destructuring to extract the values you need out of each element in the array.
1817

1918
### Expected result
2019

@@ -29,7 +28,7 @@ Albus Dumbledore
2928
## Task 2
3029

3130
- In `exercise.js` write a program that will take the `hogwarts` array as input and display the names of teachers who have pets.
32-
- Use array destructuring to extract the values you need out of the array.
31+
- Use object destructuring to extract the values you need out of each element in the array.
3332

3433
### Expected result
3534

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let order = [
2+
{ itemName: "Hot cakes", quantity: 1, unitPricePence: 232 },
3+
{ itemName: "Apple Pie", quantity: 2, unitPricePence: 139 },
4+
{ itemName: "Egg McMuffin", quantity: 1, unitPricePence: 280 },
5+
{ itemName: "Sausage McMuffin", quantity: 1, unitPricePence: 300 },
6+
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
7+
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
8+
];
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Exercise
22

3-
_Need some help? Refresh your memory with [this article](https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/)_
4-
53
- In `exercise.js`, you have been provided with a takeout order. Write a program that will print out the receipt for this order.
64
- Log each individual item to the console.
75
- Log the total cost of the order to the console.
6+
- Use object destructuring to access the values you need from each item.
7+
- Pay attention to the exact formatting of the expected result.
88

99
## Expected result
1010

1111
```
1212
QTY ITEM TOTAL
13-
1 Hot Cakes 2.29
13+
1 Hot Cakes 2.32
1414
2 Apple Pie 2.78
1515
1 Egg McMuffin 2.80
1616
1 Sausage McMuffin 3.00
1717
2 Hot Coffee 2.00
1818
4 Hash Brown 1.60
1919
20-
Total: 14.47
20+
Total: 14.50
2121
```

fetch/array-destructuring/exercise-1/readme.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

fetch/array-destructuring/exercise-3/exercise.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)