3. Short-term Memory (debug)


#1

Please help me, I don’t know what I’m doing wrong
The error messages I’m getting:
"Ops, tente outra vez. There was a problem with your syntax."
and the console is logging:
“SyntaxError: Unexpected Identifier”

The alert is about syntax, but I have no idea what I’ve written wrong (it’s probably some silly mistake but still)

var cashRegister = {
    total: 0,
//insert the add method here    
    add: function(itemCost){
        this.total += itemCost
        return total}
    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); 
        }
        return true;
    }
};

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


#2
add: function(itemCost){
        this.total += itemCost
        return total
}

you need a , after it.Also you don’t need any return statement for it.

now see here

cashRegister.scan("eggs"*2);

your scan method has one parameter named item so now you can just pass one argument.you need one more parameter.then pass two arguments.like

cashRegister.scan("eggs",2);


#3

Your correction in the method worked, (thank you VERY MUCH on spotting that missing comma!) but I’m failing at call more than one item.
Now the console logs Your bill is 0.98 , so it is not showing the sum.
I’ve written as you did here:

cashRegister.scan("eggs",2);
cashRegister.scan("magazines",3);

But I’m getting the error message:
"Have you scanned eggs twice and magazine three times?"
What could be wrong?


#4

I have just done this exercise and found this to be correct:

cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");

I am no expert so I’m afraid I can’t explain why your method would not work - perhaps someone else can help explain?


#5

Can you please post your full code here??
Also make sure you added one more parameter for scan method???then inside every case multiply that parameter by the price
For example:

 this.add(price*x)

#6

Well, that works (to be honest, it I was my first instict). However, I feel there must be another way - easier and less verbose… I mean, what if it was dozens of eggs intead of 2?
But thanks for the reply, anyway


#7

hm, reading the answer again, I feel I probably misunderstood your solution a bit at the first time, since I’ve only changed de parameters after, when calling the method.
(sorry for that, english is not my first language, and due to my inexperience in programming sometimes I get pretty confused while coding, because of the lingo)
I’m gonna try this again and then come back later with feedback


#8

now I’m gettin something totally different…

The error message says:
“Calling cashRegister.add(3.2) set the cashRegister.total to NaN instead of 3.2”

and the console is logging:
Your bill is 0

Here’s the code after I changed it:

var cashRegister = {
    total: 0,
//insert the add method here    
    add: function(itemCost,x){
        this.total += itemCost*x},
    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); 
        }
        return true;
    }
};

//Scan 2 eggs and 3 magazines
cashRegister.scan("eggs"*2);
cashRegister.scan("magazines"*3);
//Show the total bill

console.log('Your bill is '+cashRegister.total);

#9

right now I changed it to commas:

cashRegister.add("eggs",2);
cashRegister.add("magazines",3);

and the console logs
you bill is NaN

such a tricky exercise!


#10

cashRegister.add("eggs",2);

you are calling add method here.but you have to call the scan method here.what’s the point to call add method?

Don’t just write consider if you are writing a line of code then what does it do ?
look at here

add: function(itemCost,x)

why did you take x as a parameter here?you don’t need this one.your add method was fine.you just don’t need any return statement for add method.

see here

scan: function (item) {

you’ve one parameter named item for scan method.but you pass two arguments here

cashRegister.scan("eggs",2);

It doesn’t make sense.what i told to change

you need one more parameter.The value you pass as an argument sets to that parameter.you can take any name as parameter. It’s just a placeholder.

now you have to multiply the price by the second parameter what you take for scan method

this.add(0.98 * parameter_name);

i believe you know how switch case works.

cashRegister.add("magazines",3);

i’m just ignoring the add method.magazines and magazine both are different.


#11

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