-
-
Notifications
You must be signed in to change notification settings - Fork 124
GLASGOW | May-2025 | Taras Mykytiuk | Module-Data-Flows | Sprint_1 #248
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,24 @@ let hogwarts = [ | |
occupation: "Teacher", | ||
}, | ||
]; | ||
|
||
function displayGryffindor(hogwarts) { | ||
for (const person of hogwarts) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comment to task 1 - instead of getting a |
||
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); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); |
There was a problem hiding this comment.
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
.