3/7 Cryptic return of the scan? :D


#1

Hello so I noticed that return statement at the end of the scan method of the cashRegister object. Look at it.

var cashRegister = {
    total: 0,
    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;
            case "magazine":
                this.add(4.99);
                break;
            case "chocolate":
                this.add(0.45);
                break;
        }
        return true; // <-- HERE IT IS!!!
    }
};

Does it serve any purpuse? or is it a mistake? or am I just incapable of understanding basic code?


#2

In one sense it serves to raise this question, but apart from that in this program, it serves no discernible purpose. As a closing statement of a function it defines a return value that may be examined by the caller.

Consider a default case added to the switch:

    // ...
        default: return false;
    }
    return true;
};

The caller could assign a success variable to hold the return value:

var success;
// ...
success = scan('eggs');
if (!success) console.log("Item not scanned");

As an example. We would need to craft our logic so this fit the flow and permitted for correction/re-entry of the scan argument. This may have been the intention when the author created this challenge, or it may be a hint that we can do more with the logic, owing that we are left with only a working model, not a full fledged program of which this would be just a part.

Since it is a challenge, we can find teaching moments all over the place that may not be covered to any great extent, thus far, but that should seed our ambition to find more answers and new ways to use what we've acquired in the this track.


#3

ohh yea how did I not think of that.

Anyway great answer! Thanks a ton :wink:


#4

It still doesn't work for me. I don't know what the problem is though.


#5

Please post your code, the error message, and a link to the problem exercise in a new topic. Thank you.

To be sure your question is listed in the correct category, use the Q&A link at bottom left of the lesson page.