3. Short-Term Memory - why Return anything?


#1


I'm curious, what is the rationale for having the return statement at the bottom of the scan method? Thank you in advance for the response.


Link to lesson:
https://www.codecademy.com/courses/close-the-super-makert/0/3?curriculum_id=506324b3a7dffd00020bf661


    scan: function (item) {
    // switch statement
        return true;
    }


#2

@jnl2,
It is called Defensive Programming
The scan-Method is not really complete
as it does not cope with the situation when a unknown 'item' is used as an argument

I think an example
using a switch without the return true
and one with the return true will make it somewhat clearer....

reg = {
    scan: function(item,nr) {
        switch(item) {
            case "cheese":
                console.log(item);
                return true;
        }
    }
};
if ( reg.scan("cheese",2)) {
    console.log("scanned item");
}
else {
    console.log("whats up??");
}

if ( reg.scan("chees",2)) {
    console.log("scanned item");
}
else {
    console.log("whats up??");
}

console.log("=== now with return true ===");

reg = {
    scan: function(item,nr) {
        switch(item) {
            case "cheese":
                console.log(item);
                return true;
        }
        return true;
    }
};
if ( reg.scan("cheese",2)) {
    console.log("scanned item");
}
else {
    console.log("whats up??");
}

if ( reg.scan("chees",2)) {
    console.log("scanned item");
}
else {
    console.log("whats up??");
}

#3

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