Short-term memory: simple and quick question!


#1


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


Nothing in the following code is incorrect, but I'm unclear as to why return true; is necessary for this particular exercise. I'm looking back through my notes on previous chapters and I'm not able to find an explanation. Does any one have some insight?


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.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
//Show the total bill
console.log('Your bill is '+cashRegister.total);


#2

What breaks without it? (don't assume that it's necessary, people won't agree with the premise of your question)

If you've got code that lacks purpose, remove it


#3

Nothing breaks without it!

Apologizes for not being clear as to why I originally stated "it's necessary". I said this because it was part of the original code Codecademy included in this particular exercise and it gave no instructions to modify it. For that reason I made the assumption that it was necessary yet observed nothing happened when I removed it, hence my question!


#4

I'm mostly suggesting a more sceptical mindset. If you don't see/understand it yourself, don't accept it


#5

Understood! Thank you for the push to continue to question and structure code in a manner that works and makes sense to me.


#6

While I appreciate your input, would you be so kind as to address the question I asked?

Why did Codecademy include the portion of code I mentioned earlier? I believe it would be more efficient for me and anyone else attempting to learn JavaScript to not simply dismiss something just because I don't see it or understand it; we should also ask why.


#7

No really, I think it's fine to dismiss stuff (what matters isn't what you don't learn, but what you do learn) - you'd catch up to any such thing eventually anyway

I agree with you as far as that it would be better if it wasn't there to distract you

You didn't ask it until now. (Or you kind of did, but under a false premise, invalidating the question)

I could make a few decent guesses about how it came to be there, but the only thing that matters is that it has no effect. Reason about it yourself instead - how is the return value of that function used?


#8

Understood! Thank you for the advise in how I should be learning JavaScript, i.e., "what matters isn't what you don't learn, but what you do learn". This is an interesting perspective and one I certainly haven't been adopting thus far; I've spent much more time in my learning process of JavaScript trying to understand why things are being done a certain way according to Codecademy instead of just building off of what I have learned. This approach has most definitely sent me down many roads that may answer my questions (after hours of research), but ultimately changes nothing in regards to how I apply code. The question I have prompted here has actually demonstrated exactly that (How ironic! Haha!).

Without getting into the nitty-gritty details, I have discovered that return true is often used by those who are using functions in events (buttons, filling out forms, inputs, etc.). In these events return true or even return false are used to allow the event to either continue (true) or stop (false). In other words, the application of this code is pointless to this exercise or the proposed real-life scenario of this exercise.

It'll take me a little while to wrap my head around your suggested approach as it is counter-intuitive to my nature, but I'll get there. Thanks again!


#9

Definitely don't read in too much on what cc does or says, they're not very rigorous. I like to view it as easy-to-consume demonstrations of how to use a couple of languages, and then when you've got the loops and functions etc you're well enough equipped to take on other resources


#10

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