Building a Cash Register


#1

I don't really understand the purpose of returning a boolean in scan method and why you have an "OR 0" in the add method in this line => [this.total += (itemCost || 0);]


    add: function(itemCost) {
        this.total += (itemCost || 0);
        this.lastTransactionAmount = 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;
    }


#2

This is not a boolean, but a logical expression. If itemCost is a valid number that can be added to the total, it is the value added, otherwise, zero is added to the total.


#3

Thanks for your reponse, that's what I thought.

Concerning the boolean I was actually referring to the "return true" at the end of the code. Is it a way to tell that the method ran smoothly (without any error) ?


#4

The only reasonable explanation I can come up with is that the author wanted to function to have a return value other than undefined. I've been scratching my head on that one for years.

Somewhere in the old forum archives one might find a post where I explored scan validation that expanded on that. Trying to remember the post so I can search for it.


#5

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