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

JavaScript

Elizabeth Mawer
Elizabeth Mawer
9,554 Points

How do I 'total cost' items in a shopping basket?

Here is my JavaScript code to date, apologise that it is so simple I am a beginner! I'm sure this could be written with half the code but then I wouldn't understand it! lol

var A = 0; var B = 0; var C = 0; var D = 0; var total = A + B + C + D;

document.getElementById("1").onclick = name1;

function name1() { document.getElementById("product1").innerHTML = "Great Ocean Road"; quantity1(); }

function quantity1() { document.getElementById("qty1").innerHTML = "1"; cost1(); }

function cost1() { document.getElementById("price1").innerHTML = "£360.00"; quantity1(); }

document.getElementById("2").onclick = name2;

function name2() { document.getElementById("product2").innerHTML = "Blue Mountains"; cost2(); }

function cost2() { document.getElementById("price2").innerHTML = "£420.00"; quantity2(); }

function quantity2() { document.getElementById("qty2").innerHTML = "1"; amount(); }

function amount() { document.getElementById("amount").innerHTML = total; }

I can't get the values of the items inside the variables? I was then going to add them together at the end?

Ioannis Leontiadis
Ioannis Leontiadis
9,828 Points

Hello Elizabeth,

in order to get help please wrap your code as suggested from the markdown cheatsheet. You can find a link to it at the bottom of each comment or answer form.

Bryan Peters
Bryan Peters
11,996 Points

I think you need to think through the steps some more, and give us some hints on what you are trying to do, step by step. Adding some comments in your code might help us help you.

The way it reads right now:

  1. Set up vars A thru D equal to 0. Sum them in var total (total=0)
  2. When user clicks on element id #1, populate the product, quantity, and price.
  3. When user clicks on element id #2, populate the product, quantity, price. Also set the amount to total (0)