3/7 What is wrong here?


#1
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.add(eggs);
cashRegister.add(eggs);
cashRegister.add(magazines);
cashRegister.add(magazines);
cashRegister.add(magazines);
//Show the total bill
console.log('Your bill is '+cashRegister.total);

#2

I have exactly the same code, and exactly the same problem... Hmmh...


#3

two problems that it could be (i'm still quite bad so i could be wrong) are that it is cashRegister.scan("eggs");
and

add: function (itemCost) {
this.total += itemCost;
},

},

instead of

};

#4

add: function (itemCost) {
this.total += itemCost;
}; here it needs a comma


#5

This still looked unanswered and I don't know if you haven't gotten passed it, but I pasted my working code below. It looks like you have a few issues, so look for '//' for comments, there are 3:

var cashRegister = {
    total: 0,
    add: function (itemCost) {
        this.total += itemCost;
    },   // this should change to a comma ',' because a semi-colon closes it out; PROBLEM 1

    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;
    }
};
// PROBLEMS 2 and 3 below
cashRegister.scan("eggs");  // the item needs to be in quotes or it can't call it (remember 'eggs' is nothing)
cashRegister.scan("eggs");  // so be sure to add the remaining items in quotes "eggs"
cashRegister.scan("magazine");  // you also have the wrong calling method, it should be 'scan' NOT add
cashRegister.scan("magazine"); // if you do 'add' it will only add the strings "eggs" and "magazines" 
cashRegister.scan("magazine"); // not the number values

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

Hope this helps!


#6

You helped, thank you very much!
I had problem with semi-colon and it locked me yesterday.

I appreciate it :smile:


#7

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);


#8

it is showing syntax error..even with this code..i did the same thing! :frowning:


#9

Which code are you referring to? If you tried mine, I just loaded mine again and it still passes. All I did was a simple copy and paste - make sure you have the indents right or else it may not pass.

If you are having trouble, I might recommend looking at it again. I'm not an expert but I think my code is pretty efficient and follows the instructions. Now I'm worried my code isn't right even though it passes! :worried:


#10

var cashRegister = {
total: 0,
var add: function (itemCost) {
this.total+= itemCost;

},
//insert the add method here    


scan: function (item) {
    switch (item) { 
    case "eggs": 
        this.add(0.98); 
        break;
    
    case "milk": 
        this.add(1.23); 
        break;
    
    case "magazine"://Add other 2 items here
       this.add(4.99);
       break;
       
       
       case "chocolate" :
           this.add(0.45);
           break;
    
    }
    return true;
}

};

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

this is the code m talking about...
it is the same as yours..i guess!
this is not working.


#11

Really watch the indents and spaces, I feel like that's what's causing the syntax property error.

ALSO: Line 3 you have 'var add:' and I think it should only be 'add:" See if that works.

EDIT: I tried using 'var' and it failed; I tried just 'add' and it passed, same code that I used in my initial response, does that work now?

EDIT-2: My first response from 21 days ago still passes with exact copying and pasting, make sure you are using that code because the indentation and spacing in the one you provided here are different from mine.


#12

yeah..it worked!!
thanks a ton.


#13

Awesome, glad to hear it. You had me worried there for a moment, a relief that I found it. :relieved:


#14

haha..lol!!
but still thanks :smile:


#15

You have two errors:
1. comma after
add: function (itemCost) {
this.total += itemCost;
};
instead of };
2. magazine NOT magazineS in cashRegister.add(magazines);


#16

Thank you so much it works for me, and you saved me.

Thanks
Ashish Magar


#17

Glad to hear it! This was a tough one. :grinning:


#18

Dude, i made this code

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);

it's ABSOLUTELLY same as your's, but my don't working for some reason, your working. Can somone explaine where is bug? Cuz i need to understand what i'm doing wrong, if i wrong.


#19

oh god, i found, ' instead of " at Magazine. sorry )))).