Answered You can hire a professional tutor to get the answer.

QUESTION

This is case problem 2 from Tutorial 12 in New Perspectives HTML5, CSS3, and Javascript Sixth Edition by Carey.

I have to create a javascript file for a homework assignment and I am totally stumped. This is case problem 2 from Tutorial 12 in New Perspectives HTML5, CSS3, and Javascript Sixth Edition by Carey.

3. Add an event listener that runs the setupCart() function when the page is loaded.

4. Create the setupCart() function. The purpose of this function is is to define the event handlers for the Add to order Buttons on the page. Add the following commands to the function:

a. Create a variable named addButtons, which contains the collection of elements belonging to the addButton class.

b. Loop through the addButton collection and for each button, add an event handler that runs the addItem() function when the button is clicked.

5. Create the addItem() function, which adds items to the shoping cart on the page. Add the following commands to the function:

a. description of the food item is the next sibling element to the button that was clicked by the customer. use the nextElementSibling property to reference the next sibling

  element to the target of the event object. Store the sibling element in the variable foodItem.

b. Create the foodID variable - contains the value of the id attribute for foodItem

c. Use the cloneNode() method to create a copy of the foodItem element and all of its descendants - store in foodDescription variable

d. shopping cart is stored in an aside element with the ID "cart" - store reference to this in variable named cartBox

The shopping cart needs to determine whether product ordered by customer has already been ordered = to do add span element to the top of each item in the cart containing

  the # of items of each product ordered and update value when product order repeated. do following to create order counter for each product.

e. create variable duplicateOrder - set initial value to false

f. loop thru element child nodes of cartBox - for each node determine whether ID of element node equals foodID. If it does, previously placed that menu item in cart. Increase

value of the first element child of node by 1 to indicate an additional order and then break out of the for loop.

g. After the for loop has complete - test duplicateOrder is still false. if it is, create a variable orderCount - storing span element node. set text content of the orderCount element

  to 1. Insert orderCount as first child of foodDescription node structure and append foodDescription to cartBox as a new product order.

This is my code - if you could tell me what I've done wrong that would be hugely appreciated! (Sorry doesn't copy and paste with all the proper indentations and such :S

window.addEventListener("load", setupCart);

function setupCart() {

var addButtons = document.querySelectorAll("addButton");

for (var i = 0; i < addButtons.length; i++) {

addButtons[i].onclick = addItem();

}

}

function addItem(e) {

var foodItem = e.target.nextElementSibling;

var foodID = foodItem.getAttribute("id").value;

var foodDescription = foodItem.cloneNode(true);

var cartBox = document.getElementById("cart");

var duplicateOrder = false;

// Add duplicate items to cartBox

for (var i = 0; i < cartBox.childNodes.length; i++) {

if (cartBox.childNodes[i].id === foodID.id) {

cartBox.firstElementChild.value + 1;

break; //stop processing the for loop

}

}

// Add new items to cartBox

if (duplicateOrder === false) {

var orderCount = document.createElement("span");

orderCount.textContent = 1;

foodDescription.appendChild(orderCount);

cartBox.appendChild(foodDescription);

}

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question