Last active
March 9, 2022 02:26
-
-
Save frankstepanski/44acf02b7b971f19aca72bf8c787b190 to your computer and use it in GitHub Desktop.
JavaScript foundations: Looping over objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cart = { | |
"Gold Round Sunglasses": { quantity: 1, priceInCents: 1000 }, | |
"Pink Bucket Hat": { quantity: 2, priceInCents: 1260 }, | |
}; | |
// The calculateCartTotal function will take in the cart and return a total price, in cents, of everything inside of it. | |
function calculateCartTotal(cart) { | |
let grandTotal = 0; | |
for (const prop in cart) { | |
let quantity = Object.values(cart[prop])[0]; | |
let price = Object.values(cart[prop])[1]; | |
grandTotal += quantity * price; | |
} | |
return grandTotal; | |
} | |
// The printCartInventory will take in the cart and return a string, joined by \n, of the quantity and name of each item. | |
function printCartInventory(cart) { | |
let inventory = ""; | |
for (const prop in cart) { | |
inventory += `${Object.values(cart[prop])[0]}x${prop}\n` | |
} | |
return inventory; | |
} | |
console.log(calculateCartTotal(cart)); | |
console.log(printCartInventory(cart)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
very cool I got it to loop but it wasn't not looping while adding the quantity