Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion Sprint-1/destructuring/exercise-1/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const personOne = {

// Update the parameter to this function to make it work.
// Don't change anything else.
function introduceYourself(___________________________) {
function introduceYourself(person) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal in this task is to use object destructuring to solve the challenge. Can you see how to change only the parameter here (person) such that the original function works?

Hint: Don't create new variables, like accessing person.name.

const name = person.name;
const age = person.age;
const favouriteFood = person.favouriteFood;
console.log(
`Hello, my name is ${name}. I am ${age} years old and my favourite food is ${favouriteFood}.`
);
Expand Down
21 changes: 21 additions & 0 deletions Sprint-1/destructuring/exercise-2/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,24 @@ let hogwarts = [
occupation: "Teacher",
},
];

function displayGryffindor(hogwarts) {
for (const person of hogwarts) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to task 1 - instead of getting a person and then accessing a property like .house, can you use object destructuring in the for-of loop?

if (person.house == "Gryffindor") {
console.log(person.firstName + " " + person.lastName);
}
}
}

function displayTeachersWithPet(hogwarts) {
for (const person of hogwarts) {
if (person.occupation == "Teacher" && person.pet != null) {
console.log(person.firstName + " " + person.lastName);
}
}
}
console.log("Gryffindor members:")
displayGryffindor(hogwarts);

console.log("Teachers with a pet:")
displayTeachersWithPet(hogwarts);
59 changes: 59 additions & 0 deletions Sprint-1/destructuring/exercise-3/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,62 @@ let order = [
{ itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 },
{ itemName: "Hash Brown", quantity: 4, unitPricePence: 40 },
];

function printOrder(order) {
let maxNumberLength = findLongestQuantityNumberLength(order);
if (maxNumberLength < 3) {
// length of 'QTY' word is 3
maxNumberLength = 3;
}
let maxNameLength = findLongestItemNameLength(order);
if (maxNameLength < 4) {
// length of 'ITEM' word is 3
maxNameLength = 4;
}
console.log("QTY" + " ".repeat(4) + "ITEM" + " ".repeat(maxNameLength) + "TOTAL");
for (const item of order) {
console.log(item.quantity + " ".repeat(maxNumberLength - numberDigitsQuantity(item.quantity) + 4) + item.itemName + " ".repeat(maxNameLength - item.itemName.length + 4) + convertPenceToPounds(item.unitPricePence));
}
}

// number of digits can be calculated from logarithm with base 10
function numberDigitsQuantity(number) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may be overthinking this task - we don't need something that is absolutely perfectly formatted, rather we are trying to build a function that can incorporate object destructuring to avoid having to keep accessing properties on the objects

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. I have added destructuring to each task now.

const logarithm = Math.log10(number);
const digitsQuantity = Math.floor(logarithm) + 1;
return digitsQuantity;
}

function findLongestQuantityNumberLength(order) {
let max = 0;
for (const item of order) {
const numberLength = numberDigitsQuantity(item.quantity)
if (numberLength > max) {
max = numberLength;
}
}
return max;
}

function findLongestItemNameLength(order) {
let max = 0;
for (const item of order) {
if (item.itemName.length > max) {
max = item.itemName.length;
}
}
return max;
}

function convertPenceToPounds(pence) {
const penceString = String(pence);
const pounds = penceString.substring(
0,
penceString.length - 2
);
const remainingPence = penceString
.substring(penceString.length - 2);

return `${pounds.padStart(1, "0")}.${remainingPence}`;
}

printOrder(order);