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

Pablo Calvo
Pablo Calvo
13,044 Points

SCORE VARIABLE

Hello ,

Guys im trying to create a simple variable to keep a sales score... I've tried the following:

const x = 1; x += x, this gives me 2 x = ++x, this gives me 3 as result

what im trying to do is :

Const x = 0. when a new sale comes along I need to be able to add the price of the current sale to what's already stored in that variable, any help is greatly appreciated...

thanks

2 Answers

You should never use a const keyword to declare a variable that will change. The const keyword means = constant, which means whatever value you give to that will stay the same and never change. An alternative to this is the let keyword which can change and is what you should use in this case. So here is an example:

let saleValue = 0; // starts with a 0 value until some values are added to it

let firstSale = 5; 
let secondSale = 15;

saleValue = firstSale + secondSale; 

alert(saleValue); // this line will show a box on screen with the final value
Pablo Calvo
Pablo Calvo
13,044 Points

Thanks for replying, alright got it yes, no const for this,... now say that you're getting the sale price from an input in html, like this:

let productPrice = document.getElementById('priceinput') // this points to a text input field in HTML

so... everytime there is a new sale, it is the same variable, productPrice that gets the new value of the new sale... so... if the current score is 100 and the new sale is 200 the result im looking for is that variable to hold now a value of 300,...values can't be gathered statically as in your example aboive since what im trying to do is dinamically update that value,...

Instead what's happening is everytime a new sale comes along the value of the variable gets refreshed to the new sale's value, not really keeping the sales's score.,... check my code please, thanks:

saveButton.addEventListener('click', () => { console.log('HELLO'); const productList = document.getElementById('productField'); let productprice = document.getElementById('productPriceField') const productListdisplay = productList.value; const displayNewLi = document.createElement('li'); displayNewLi.setAttribute('class', 'totalsalestest') let salesScore = parseInt(productprice.value);// THIS IS ME TRYING TO DO WHAT I EXPLAINED ABOVE displayNewLi.textContent = newSaleInput.value + " " + productListdisplay + " $ " + productprice.value; totalSales.appendChild(displayNewLi); existingSalesUl.removeChild(newLi); console.log(salesScore);

});

Really appreciate the help... thanks