Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Paccione
9,017 PointsRetrieve Integer from mixed Javascript array
I'm making a pizza ordering page with Javascript. I think I've made a misstep when building this because what I want to do may not be possible.
I have an array "orderArray" that contains strings with integers in them for the price of the pizza order. I need to retrieve these values and add them to create the total for the payment.
If what I am asking isn't clear enough I can host it so you can see the problem visually. Here is the whole script.
//Global Vars
var button = document.getElementsByTagName('button')[0];
var form2 = document.getElementById('form2');
var form3 = document.getElementById('form3');
var tax = 0.0875;
var orderText;
var subtotal;
var total;
var orderArray = [];
var n = 1;
//Form Button
button.addEventListener("click", function(){
//Local Selection Vars
var sizeChecked = document.querySelector('input[name="size"]:checked');
var crustChecked = document.querySelector('input[name="crust"]:checked');
var toppingsChecked = document.querySelector('input[name="toppings"]:checked');
var breadsticksChecked = document.querySelector('input[name="breadsticks"]:checked');
//Adding Cost
subtotal = parseInt(sizeChecked.value) + parseInt(crustChecked.value) + parseInt(toppingsChecked.value) + parseInt(breadsticksChecked.value);
total = Math.round((subtotal + (subtotal * tax)) * 100) / 100;
//Container Var for Selected Menu Items
orderText = sizeChecked.getAttribute('data-size')+' '+ crustChecked.getAttribute('data-crust')+' crust pizza topped with ' +
toppingsChecked.getAttribute('data-toppings')+' '+breadsticksChecked.getAttribute('data-breadsticks');
//Pushing Cost and Selected Items to Array
orderArray.push('<p>Order '+n+': $'+subtotal+'</p>');
orderArray.push('<p>One '+orderText+'.</p>');
orderArray.toString();
//Form2 HTML template with order array
form2.innerHTML = '<p class="underline textcenter">Order</p>'+'<p>'+orderArray+'</p>'+'<br><button id="buttonCheckout">Add to Cart</button>';
//Updating Order Number for next order
n++;
//Local Button Vars
var button2 = document.getElementById('buttonCheckout');
var button3 = document.getElementById('buttonPay');
//Form2 Button
button2.addEventListener("click", function(){
console.log('button2 click');
form3.innerHTML = '<p class="underline textcenter">Pay</p><p>Subtotal: $'+subtotal+'</p><p>Tax: $'+(subtotal * tax)+
'</p><p>Total: $'+total+'</p><br><button id="buttonPay">Make Payment</button>';
});
});
2 Answers
Kevin VanderWulp
5,180 PointsMaybe I'm mistaken, but it looks like you've already added the total and stored it in a global variable. If you're trying to add up multiple pizza's, and your script is just for a single pizza, you could keep adding to this global var. Another way would be to create a pizza object that contains the total for each pizza that has a function to return the value of each pizza.
Victor Rundbaken
14,037 PointsWhy not just add up the total prior to turning it into a string?
If you can't do that you could make a loop to check each index for a $ sign in the string and then make a sub string that cuts off everything prior to and including the $ sign leaving the string starting with the numbers and then parseInt(). It's not exactly pretty but it would give you the int value after the dollar sign.
Use index of to find the $ sign https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
Michael Paccione
9,017 PointsMichael Paccione
9,017 PointsThanks guys, sometimes I am at it too long and just need to step back and take a break.