From b04cb081d214c13999308b9260245601b84fc32e Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Thu, 17 Oct 2024 13:52:05 +0100 Subject: [PATCH] Rewrite "array destructuring" exercises as object destructuring 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. --- .../destructuring}/exercise-1/exercise.js | 2 ++ Sprint-1/destructuring/exercise-1/readme.md | 33 +++++++++++++++++++ .../destructuring}/exercise-2/exercise.js | 0 .../destructuring}/exercise-2/readme.md | 7 ++-- Sprint-1/destructuring/exercise-3/exercise.js | 8 +++++ .../destructuring}/exercise-3/readme.md | 8 ++--- .../array-destructuring/exercise-1/readme.md | 21 ------------ .../exercise-3/exercise.js | 8 ----- 8 files changed, 50 insertions(+), 37 deletions(-) rename {fetch/array-destructuring => Sprint-1/destructuring}/exercise-1/exercise.js (76%) create mode 100644 Sprint-1/destructuring/exercise-1/readme.md rename {fetch/array-destructuring => Sprint-1/destructuring}/exercise-2/exercise.js (100%) rename {fetch/array-destructuring => Sprint-1/destructuring}/exercise-2/readme.md (71%) create mode 100644 Sprint-1/destructuring/exercise-3/exercise.js rename {fetch/array-destructuring => Sprint-1/destructuring}/exercise-3/readme.md (71%) delete mode 100644 fetch/array-destructuring/exercise-1/readme.md delete mode 100644 fetch/array-destructuring/exercise-3/exercise.js diff --git a/fetch/array-destructuring/exercise-1/exercise.js b/Sprint-1/destructuring/exercise-1/exercise.js similarity index 76% rename from fetch/array-destructuring/exercise-1/exercise.js rename to Sprint-1/destructuring/exercise-1/exercise.js index a6eab299..1ff2ac5c 100644 --- a/fetch/array-destructuring/exercise-1/exercise.js +++ b/Sprint-1/destructuring/exercise-1/exercise.js @@ -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}.` diff --git a/Sprint-1/destructuring/exercise-1/readme.md b/Sprint-1/destructuring/exercise-1/readme.md new file mode 100644 index 00000000..28ca6c3d --- /dev/null +++ b/Sprint-1/destructuring/exercise-1/readme.md @@ -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. diff --git a/fetch/array-destructuring/exercise-2/exercise.js b/Sprint-1/destructuring/exercise-2/exercise.js similarity index 100% rename from fetch/array-destructuring/exercise-2/exercise.js rename to Sprint-1/destructuring/exercise-2/exercise.js diff --git a/fetch/array-destructuring/exercise-2/readme.md b/Sprint-1/destructuring/exercise-2/readme.md similarity index 71% rename from fetch/array-destructuring/exercise-2/readme.md rename to Sprint-1/destructuring/exercise-2/readme.md index 0cfec3b0..64b5ab1c 100644 --- a/fetch/array-destructuring/exercise-2/readme.md +++ b/Sprint-1/destructuring/exercise-2/readme.md @@ -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 @@ -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 @@ -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 diff --git a/Sprint-1/destructuring/exercise-3/exercise.js b/Sprint-1/destructuring/exercise-3/exercise.js new file mode 100644 index 00000000..b3a36f4e --- /dev/null +++ b/Sprint-1/destructuring/exercise-3/exercise.js @@ -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 }, +]; diff --git a/fetch/array-destructuring/exercise-3/readme.md b/Sprint-1/destructuring/exercise-3/readme.md similarity index 71% rename from fetch/array-destructuring/exercise-3/readme.md rename to Sprint-1/destructuring/exercise-3/readme.md index d2172a30..9663f01b 100644 --- a/fetch/array-destructuring/exercise-3/readme.md +++ b/Sprint-1/destructuring/exercise-3/readme.md @@ -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 ``` diff --git a/fetch/array-destructuring/exercise-1/readme.md b/fetch/array-destructuring/exercise-1/readme.md deleted file mode 100644 index c67ec4f9..00000000 --- a/fetch/array-destructuring/exercise-1/readme.md +++ /dev/null @@ -1,21 +0,0 @@ -Did you know that destructuring can also be used on objects as well. - -We can use 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`. Notice how we use the `{}` characters since it is an object rather than `[]` which is used when it is an array. - -# Exercise - -- What is the syntax to destructure the object `personOne` in exercise.js? -- Update the argument of the function `introduceYourself` to use destructuring on the object that gets passed in. diff --git a/fetch/array-destructuring/exercise-3/exercise.js b/fetch/array-destructuring/exercise-3/exercise.js deleted file mode 100644 index 0a01f8f0..00000000 --- a/fetch/array-destructuring/exercise-3/exercise.js +++ /dev/null @@ -1,8 +0,0 @@ -let order = [ - { itemName: "Hot cakes", quantity: 1, unitPrice: 2.29 }, - { itemName: "Apple Pie", quantity: 2, unitPrice: 1.39 }, - { itemName: "Egg McMuffin", quantity: 1, unitPrice: 2.8 }, - { itemName: "Sausage McMuffin", quantity: 1, unitPrice: 3.0 }, - { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, - { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, -];