Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit 31c1abb

Browse files
author
Chris Owen
authored
Merge pull request #2 from CodeYourFuture/js3-3-destructuring
Add Array Destructuring Homework
2 parents f56d396 + 405bbc2 commit 31c1abb

File tree

11 files changed

+118
-131
lines changed

11 files changed

+118
-131
lines changed

week-3/Homework/mandatory/2-exercises/1-shopping-cart.js

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

week-3/Homework/mandatory/2-exercises/2-convertion.js

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

week-3/Homework/mandatory/2-exercises/3-atm.js

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

week-3/Homework/mandatory/2-exercises/4-music-player.js

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const personOne = {
2+
name: 'Popeye',
3+
age: 34,
4+
favouriteFood: 'Spinach'
5+
}
6+
7+
function introduceYourself(___________________________) {
8+
console.log (`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`);
9+
}
10+
11+
introduceYourself(personOne);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Did you know that destructuring can also be used on objects as well.
2+
3+
We can use destructuring to extract values from an object and assign them to variables in one line.
4+
5+
```
6+
let person = {
7+
firstName: "Bruce",
8+
lastName: "Wayne"
9+
}
10+
11+
let { firstName, lastName } = person;
12+
13+
console.log(`Batman is ${firstName}, ${lastName}`);
14+
```
15+
16+
The program above will print `Batman is Bruce Wayne`. Notice how we use the `{}` characters since it is an object rather than `[]` which is used when it is an array.
17+
18+
# Exercise
19+
20+
- What is the syntax to destructure the object `personOne` in exercise-1.js?
21+
- Update the argument of the function `introduceYourself` to use destructuring on the object that gets passed in.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let hogwarts = [
2+
{ firstName: "Harry", lastName: "Potter", house: "Gryffindor", pet: "Owl", occupation: "Student" },
3+
{ firstName: "Hermione", lastName: "Granger", house: "Gryffindor", pet: "Cat", occupation: "Student" },
4+
{ firstName: "Draco", lastName: "Malfoy", house: "Slytherin", pet: null, occupation: "Student" },
5+
{ firstName: "Cedric", lastName: "Diggory", house: "HufflePuff", pet: null, occupation: "Student" },
6+
{ firstName: "Severus", lastName: "Snape", house: "Slytherin", pet: null, occupation: "Teacher" },
7+
{ firstName: "Filius", lastName: "Flitwick", house: "Ravenclaw", pet: null, occupation: "Teacher" },
8+
{ firstName: "Pomona", lastName: "Sprout", house: "Hufflepuff", pet: null, occupation: "Teacher" },
9+
{ firstName: "Minerva", lastName: "McGonagall", house: "Gryffindor", pet: null, occupation: "Teacher" },
10+
{ firstName: "Albus", lastName: "Dumbledore", house: "Gryffindor", pet: "Phoenix", occupation: "Teacher" }
11+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Exercise 2
2+
3+
_Need some help? Refresh your memory with [this article](https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/)_
4+
5+
In `exercise-2.js`, you have an array that contains a list of people who are at Hogwarts School of Witchcraft and Wizardry.
6+
For each character you have the following information:
7+
8+
- First Name
9+
- Last Name
10+
- School House
11+
- Pet
12+
- Occupation
13+
14+
## Task 1
15+
16+
- In `exercise-2.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.
18+
19+
### Expected result
20+
21+
```
22+
Harry Potter
23+
Ron Weasley
24+
Hermione Granger
25+
Minerva McGonagall
26+
Albus Dumbledore
27+
```
28+
29+
## Task 2
30+
31+
- In `exercise-2.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.
33+
34+
### Expected result
35+
36+
```
37+
Albus Dumbledore
38+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let order = [
2+
{ itemName: "Hot cakes", quantity: 1, unitPrice: 2.29},
3+
{ itemName: "Apple Pie", quantity: 2, unitPrice: 1.39},
4+
{ itemName: "Egg McMuffin", quantity: 1, unitPrice: 2.80},
5+
{ itemName: "Sausage McMuffin", quantity: 1, unitPrice: 3.00},
6+
{ itemName: "Hot Coffee", quantity: 2, unitPrice: 1.00},
7+
{ itemName: "Hash Brown", quantity: 4, unitPrice: 0.40}
8+
]
9+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Exercise
2+
3+
_Need some help? Refresh your memory with [this article](https://www.freecodecamp.org/news/array-destructuring-in-es6-30e398f21d10/)_
4+
5+
- In `exercise-3.js`, you have been provided with a takeout order. Write a program that will print out the receipt for this order.
6+
- Log each individual item to the console.
7+
- Log the total cost of the order to the console.
8+
9+
## Expected result
10+
11+
```
12+
QTY ITEM TOTAL
13+
1 Hot Cakes 2.29
14+
2 Apple Pie 2.78
15+
1 Egg McMuffin 2.80
16+
1 Sausage McMuffin 3.00
17+
2 Hot Coffee 2.00
18+
4 Hash Brown 1.60
19+
20+
Total: 14.47
21+
```

0 commit comments

Comments
 (0)