Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite "array destructuring" exercises as object destructuring #47

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const personOne = {
favouriteFood: "Spinach",
};

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
console.log(
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`
Expand Down
33 changes: 33 additions & 0 deletions Sprint-1/destructuring/exercise-1/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
We can use object destructuring to extract values from an object and assign them to variables in one line.

```js
let person = {
firstName: "Bruce",
lastName: "Wayne",
};

let { firstName, lastName } = person;

console.log(`Batman is ${firstName}, ${lastName}`);
```

The program above will print `Batman is Bruce Wayne`.

This is more concise than doing this without object destructuring:

```js
let person = {
firstName: "Bruce",
lastName: "Wayne",
};

let firstName = person.firstName;
let lastName = person.lastName;

console.log(`Batman is ${firstName}, ${lastName}`);
```

# Exercise

- What is the syntax to destructure the object `personOne` in exercise.js?
- Update the parameter of the function `introduceYourself` to use destructuring on the object that gets passed in.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Exercise 2

_Need some help? Refresh your memory with [this article](https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/)_

In `exercise.js`, you have an array that contains a list of people who are at Hogwarts School of Witchcraft and Wizardry.

For each character you have the following information:

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

- 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.
- Use array destructuring to extract the values you need out of the array.
- Use object destructuring to extract the values you need out of each element in the array.

### Expected result

Expand All @@ -29,7 +28,7 @@ Albus Dumbledore
## Task 2

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

### Expected result

Expand Down
8 changes: 8 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let order = [
{ itemName: "Hot cakes", quantity: 1, unitPricePence: 232 },
{ itemName: "Apple Pie", quantity: 2, unitPricePence: 139 },
{ itemName: "Egg McMuffin", quantity: 1, unitPricePence: 280 },
{ itemName: "Sausage McMuffin", quantity: 1, unitPricePence: 300 },
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# Exercise

_Need some help? Refresh your memory with [this article](https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/)_

- In `exercise.js`, you have been provided with a takeout order. Write a program that will print out the receipt for this order.
- Log each individual item to the console.
- Log the total cost of the order to the console.
- Use object destructuring to access the values you need from each item.
- Pay attention to the exact formatting of the expected result.

## Expected result

```
QTY ITEM TOTAL
1 Hot Cakes 2.29
1 Hot Cakes 2.32
2 Apple Pie 2.78
1 Egg McMuffin 2.80
1 Sausage McMuffin 3.00
2 Hot Coffee 2.00
4 Hash Brown 1.60

Total: 14.47
Total: 14.50
```
21 changes: 0 additions & 21 deletions fetch/array-destructuring/exercise-1/readme.md

This file was deleted.

8 changes: 0 additions & 8 deletions fetch/array-destructuring/exercise-3/exercise.js

This file was deleted.