-
-
Couldn't load subscription status.
- Fork 83
NW6 | Rabia Avci | JS3 Module | [TECH ED] Programmer Humour | Week 3 #315
base: main
Are you sure you want to change the base?
Changes from all commits
e4c7725
911d2f8
e94e728
3daf9a7
d50128b
4620836
3614e1b
2495a8f
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 |
|---|---|---|
|
|
@@ -6,3 +6,19 @@ let order = [ | |
| { itemName: "Hot Coffee", quantity: 2, unitPrice: 1.0 }, | ||
| { itemName: "Hash Brown", quantity: 4, unitPrice: 0.4 }, | ||
| ]; | ||
|
|
||
| function formatPrice(price) { | ||
| return price.toFixed(2); | ||
| } | ||
|
|
||
| console.log("QTY\tITEM\t\t\tTOTAL"); | ||
|
|
||
| let total = 0; | ||
|
|
||
| for (const {quantity, itemName, unitPrice} of order) { | ||
| console.log(`${quantity}\t${itemName}\t\t${(unitPrice * quantity).toFixed(2)}`); | ||
| total += unitPrice * quantity; | ||
| } | ||
|
|
||
| console.log("\nTotal:", formatPrice(total)); | ||
|
|
||
|
Comment on lines
+10
to
+24
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. Well done finding a solution! Also, an improvement you could make to this solution would be to use Array.reduce() to create a running aggregate instead of defining totalPrice outside of the loop. Here is a link that explains using reduce to calculate a sum. https://linuxhint.com/call-reduce-on-an-array-of-objects-to-sum-their-properties/ Also, you have used |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
|
|
||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Programmer Humour</title> | ||
| <link rel="stylesheet" href="styles.css" /> | ||
| <script defer src="script.js"></script> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div class="container"> | ||
| <h1>xkcd Comic Viewer</h1> | ||
| <div id="comicContainer"> | ||
| </div> | ||
| <footer> | ||
| <p>Comic made by Randall Munroe from <a rel="noopener" href="https://xkcd.com/" target="_blank">xkcd.com</a> | ||
| </p> | ||
| <p>page by Rabia AVCI <a rel="noopener" href="https://github.com/RbAvci" target="_blank">GitHub</a> | ||
| </p> | ||
| </footer> | ||
| </div> | ||
|
|
||
| </body> | ||
|
|
||
|
|
||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| document.addEventListener("DOMContentLoaded", function () { | ||
| fetchComic(); | ||
| }); | ||
|
|
||
| function fetchComic() { | ||
| fetch("https://xkcd.now.sh/?comic=latest") | ||
| .then((response) => response.json()) | ||
| .then((data) => { | ||
| console.log(data); | ||
|
|
||
| const comicContainer = document.getElementById("comicContainer"); | ||
| const imgElement = document.createElement("img"); | ||
| imgElement.src = data.img; | ||
| imgElement.alt = data.alt; | ||
| comicContainer.appendChild(imgElement); | ||
| }) | ||
| .catch((error) => console.error("Error fetching comic:", error)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| body { | ||
| font-family: Arial, sans-serif; | ||
| margin: 20px; | ||
| } | ||
|
|
||
| .container { | ||
| max-width: 800px; | ||
| margin: 0 auto; | ||
| text-align: center; | ||
| } | ||
|
|
||
| h1 { | ||
| color: aliceblue; | ||
| max-width: 100%; | ||
| padding: 10px; | ||
| background-color: #c24242; | ||
|
|
||
| } | ||
|
|
||
| img { | ||
| max-width: 100%; | ||
| height: auto; | ||
| box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | ||
| background-color: #c24242; | ||
| padding: 2rem; | ||
|
|
||
| } | ||
|
|
||
| footer { | ||
| color: aliceblue; | ||
| padding: 1px; | ||
| max-width: 100%; | ||
| border: solid 2px #c24242; | ||
| margin-top: 10px; | ||
| background-color: #c24242; | ||
|
|
||
| } | ||
|
Comment on lines
+12
to
+37
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. It is not advisable to set CSS values on generic HTML elements such as img, div, h1, h2 etc. Doing this will mean you will apply these styles to every element you create. Instead, use classNames on HTML elements to apply styling to specific elements without affecting anything that doesn't have this className. 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 Jack, yes that's good advice for me. I thought it was a small project and that it was ok to use like this, I will consider it from now on. Thank you! |
||
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.
Perfect!
Well done