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

why is my variable not decrementing in my buyNow() function

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>Aso Rock shopping list</h1>

   <input id="selection" list="browsers">
   <datalist id="browsers">
        <option value="Jollof Rice">
        <option value="Coke">
        <option value="Chrome">
        <option value="Opera">
        <option value="Safari">
  </datalist> 
  <input type="submit" onclick="getSelectionInfo()"><br>

  <button id="buy" onclick="buyNow()"> </button>

    <script src="app.js"></script>
</body>

</html>

app.js

function foodInfo(type,price,quantity){
        this.type=type
        this.price=price
        this.quantity=quantity
}


var riceMeal = new foodInfo ('Jollof Rice',7.50,5)
var coke = new foodInfo('Coke',0.6,24)

var stockList =[riceMeal,coke]

var selection

var selected

var shoppingkart =[]



function getSelectionInfo() {

    selection = document.getElementById('selection').value

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

    if(selection==stockList[i].type){

    selected= document.getElementById('buy').innerHTML=stockList[i].type+'£'+stockList[i].price

        break
    }

  }
}


function buyNow(){

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

    if(selected==stockList[i].type+'£'+stockList[i].price){
          stockList[i].quantity-1 //// ive been checking the value in the console after this function is executed and its not going down 
            break
    }

}


}

2 Answers

Steven Parker
Steven Parker
243,656 Points

Your issue seems to be on this line:

          stockList[i].quantity-1 //// ive been checking the value ... its not going down 

This is an expression, not a statement. It computes the new value but it doesn't store it anywhere. You probably intended to use a subtraction assignment here instead of a simple subtraction:

          stockList[i].quantity -= 1;  // this will reduce the quantity by one

And another way to do the same thing would be with the decrement operator:

          stockList[i].quantity--;    // this will reduce the quantity by one

Thank you again Steven

It looks like you are trying to decrement the array item property of quantity. The problem is that you aren't assigning the decremented value anywhere, so the "stockList[i].quantity-1" resolves, but isn't assigned or used anywhere, so it isn't stored for you to access later.

We need to assign the result to some variable in memory, for example, the original property that we are trying to reference. Perhaps try the following:

 stockList[i].quantity = stockList[i].quantity-1;