Last active
November 28, 2018 17:37
-
-
Save brittanydionigi/639a7d579d623a5baa72a3fa03f18df5 to your computer and use it in GitHub Desktop.
Object & Array Practice
This file contains hidden or 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
// HTML SECTION | |
<h1>Papa Turing's Pizza</h1> | |
<h2>Review Your Order</h2> | |
<ul id="order-details"></ul> | |
<button id="submit-order">Place Order</button> | |
// JAVASCRIPT SECTION | |
let pizza = { | |
orderNumber: 123, | |
size: 14, | |
delivery: true, | |
extraCheese: true, | |
toppings: ['onions', 'garlic', 'peppers'] | |
}; | |
// Object.keys() returns an array containing | |
// each property of the object you specify | |
let orderDetails = Object.keys(pizza); | |
// Arrays have a prototype method that allows you | |
// to iterate through them as well: forEach | |
orderDetails.forEach(key => { | |
let ulElem = document.getElementById('order-details'); | |
let newLiElem = document.createElement('li'); | |
newLiElem.innerText = `${key}: ${pizza[key]}`; | |
ulElem.appendChild(newLiElem); | |
}); | |
// Pizza time! | |
let submitButton = document.getElementById('submit-order'); | |
submitButton.addEventListener('click', (event) => { | |
let ulElem = document.getElementById('order-details'); | |
ulElem.innerHTML = ''; | |
// 1. Create a new object named 'order' that includes: | |
// a timestamp (the time the order was placed), | |
// a delivery property (either true/false), | |
// an order number (can be an arbitrary integer like 123456) | |
// 2. Merge the `order` object with the `pizza` object | |
// 3. Append each key/value pair from your newly merged object to the <ul> | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment