Skip to content

Commit 7b324bd

Browse files
committed
Add homework exercises for array destructuring + remove class exercises
1 parent f56d396 commit 7b324bd

File tree

10 files changed

+104
-131
lines changed

10 files changed

+104
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
- What is the syntax to destructure the object `personOne` in exercise-1.js?
20+
- Update the argument of the function `introduceYourself` to use destructuring on the object that gets passed in.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let hogwarts = [
2+
['Harry', 'Potter', 'Gryffindor', 'Owl', 'Student'],
3+
['Hermione', 'Granger', 'Gryffindor', 'Cat', 'Student'],
4+
['Draco', 'Malfoy', 'Slytherin', 'None', 'Student'],
5+
['Cedric', 'Diggory', 'HufflePuff', 'None', 'Student'],
6+
['Severus', 'Snape', 'Slytherin', 'None', 'Teacher'],
7+
['Filius', 'Flitwick', 'Ravenclaw', 'None', 'Teacher'],
8+
['Pomona', 'Sprout', 'Hufflepuff', 'None', 'Teacher'],
9+
['Minerva', 'McGonagall', 'Gryffindor', 'None', 'Teacher'],
10+
['Albus', 'Dumbledore', 'Gryffindor', 'Phoenix', 'Teacher']
11+
]
12+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
In `exercise-2.js`, you have an array that contains a list of people who are at Hogwarts School of Witchcraft and Wizardry.
3+
For each character you have the following information:
4+
- First Name
5+
- Last Name
6+
- School House
7+
- Pet
8+
- Occupation
9+
10+
11+
# Exercise 1
12+
- In `exercise-2.js` write a program that will display the names of the people who belong to the Gryffindor house.
13+
- Use array destructuring to extract the values you need out of the array.
14+
15+
## Expected result
16+
```
17+
Harry Potter
18+
Ron Weasley
19+
Hermione Granger
20+
Minerva McGonagall
21+
Albus Dumbledore
22+
```
23+
24+
25+
# Exercise 2
26+
- In `exercise-2.js` write a program that will display the names of teachers who have pets.
27+
- Use array destructuring to extract the values you need out of the array.
28+
29+
## Expected result
30+
```
31+
Albus Dumbledore
32+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let order = [
2+
['Hot cakes', 1, 2.29],
3+
['Apple Pie', 2, 1.39],
4+
['Egg McMuffin', 1, 2.80],
5+
['Sausage McMuffin', 1, 3.00],
6+
['Hot Coffee', 2, 1.00],
7+
['Hash Brown', 4, 0.40]
8+
]
9+
10+
// Write your code below
11+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Exercise
2+
- In `exercise-3.js`, you have been provided with a takeout order. Write a program that will print out the receipt for this order.
3+
- Use array destructuring to extract the values you need from the order
4+
- Log each individual item to the console.
5+
- Log the total cost of the order to the console.
6+
7+
## Expected result
8+
```
9+
QTY ITEM TOTAL
10+
1 Hot Cakes 2.29
11+
2 Apple Pie 2.78
12+
1 Egg McMuffin 2.80
13+
1 Sausage McMuffin 3.00
14+
2 Hot Coffee 2.00
15+
4 Hash Brown 1.60
16+
17+
Total: 14.47
18+
```

0 commit comments

Comments
 (0)