Why scan function not working building a cash register


#1

var cashRegister = {
    total: 0,
//insert the add method here    
    add:function(itemCost){
        this.total+=itemCost;
    },
    
    scan:function (item) {
        switch (item) { 
        case "eggs": 
            this.add(0.98); 
            break;
        
        case "milk": 
            this.add(1.23); 
            break;
        
        //Add other 2 items here
        case "magazine":
            this.add(4.99);
            break;
        case "chocolate": 
            this.add(0.45); 
            break;
        
        
        }
        return true;
    }
};

//Scan 2 eggs and 3 magazines
cashRegister.total=2*cashRegister.scan("eggs")+3*cashRegister.scan("chocolate");
//Show the total bill
console.log('Your bill is '+cashRegister.total);

#2

the scan method will increase the total, so don't store the result of the function calls in a variable

for now, you will need to make 5 separate function calls, you will improve this in the next exercise

this:

2*cashRegister.scan("eggs")

won't work, it will call the method (which returns true) and multiply 2 with returned result by the method (true) which results in 2 (2*true is 2 in JS)


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.