-
-
Notifications
You must be signed in to change notification settings - Fork 142
NW | 25-ITP-Sep | TzeMing Ho | Sprint 1 | feature/destructuring #324
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
4858927
8858480
bacdb7c
141a115
951c77b
2b13ee3
4aca32f
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,19 @@ let hogwarts = [ | |
| occupation: "Teacher", | ||
| }, | ||
| ]; | ||
|
|
||
|
|
||
| function gryffindorHouse(hogwarts) { | ||
| return hogwarts.filter(({house}) => house === 'Gryffindor').map(({firstName, lastName}) => `${firstName} ${lastName}`).join("\n") | ||
| } | ||
|
|
||
| function teachersWithPets(hogwarts) { | ||
| return hogwarts.reduce((nameList, {firstName, lastName, pet, occupation}) => { | ||
|
||
| if (occupation === "Teacher" && pet !== null) { | ||
| nameList.push(`${firstName} ${lastName}`) | ||
| } | ||
| return nameList; | ||
| }, []).join("\n") | ||
| } | ||
|
|
||
| console.log(gryffindorHouse(hogwarts)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,33 @@ let order = [ | |
| { itemName: "Hot Coffee", quantity: 2, unitPricePence: 100 }, | ||
| { itemName: "Hash Brown", quantity: 4, unitPricePence: 40 }, | ||
| ]; | ||
|
|
||
|
|
||
| function receiptAndCosts(order) { | ||
| let total = 0; | ||
|
|
||
| let receipt = [] | ||
|
|
||
| function orderLineFormatting(...args) { | ||
| const orderLineSpacing = [8, 20, 5] | ||
| let formattedOrderLine = '' | ||
| for (let index = 0; index < args.length; index++) { | ||
| formattedOrderLine += `${String(args[index]).padEnd(orderLineSpacing[index], " ")}` | ||
|
Member
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. Cute use of args! One disadvantage of using
Author
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. Would an error message when the length of arguments is not equal to 3 be good enough for users? 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. yes, i think that'd be sufficient :) (don't forget the comment for the function!)
Author
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. I have added a comment for the function. |
||
| } | ||
| return formattedOrderLine | ||
| } | ||
|
|
||
| receipt.push(orderLineFormatting("QTY", "ITEM", "TOTAL")); | ||
|
|
||
| order.forEach(({itemName, quantity, unitPricePence}) => { | ||
| const subTotal = (unitPricePence / 100 * quantity).toFixed(2) | ||
| receipt.push(orderLineFormatting(quantity, itemName, subTotal)) | ||
| total += unitPricePence / 100 * quantity | ||
| }) | ||
|
|
||
| receipt.push(" ") | ||
| receipt.push(`Total: ${total.toFixed(2)}`) | ||
| return receipt.join("\n") | ||
| } | ||
|
|
||
| console.log(receiptAndCosts(order)) | ||
Uh oh!
There was an error while loading. Please reload this page.