3/7 Short-Term Memory


#1

I've searched through as many posts as I could and modified my code, but I'm still getting a syntax error: unexpected identifier. Any help is greatly appreciated!

var cashRegister = {
total: 0,
//insert the add method here
function add(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

Hey @mbizev, the way you initalized add function was wrong. Here:

You correctly initalized your second function scan though. Try learning from that. I cant tell you the full anwser but its just a mismatch. :no_mouth:


#3

@amanuel2 , is there an easier way to execute this code?
cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");

like: cashRegister.scan(2 * "eggs" + 3 * "magazine"); This obviously doesn't work but why? Seems like a DRY situation.


#4

You're right and a later exercise does actually address exactly this!


#5

If you want to test your self you could just make a function. Let me make one for you:

function scaningItems(scanName, amount, scanNameTwo, amountTwo) {
    for (i = 0; i <= amount; i++) {
        cashRegister.scan(scanName)
    }
    for (i = 0; i <= amountTwo; i++) {
        cashRegister.scan(scanNameTwo)
    }
}

#6

Wouldn't it be simpler to accept just one item and an amount?

Accepting two seems like a limitation for how that function can be used.


#7

your mistake is in wrong declaring function in object.
you - function add(itemCost)
correct - add:functiond(itemCost) { }


#8

@mbizev

I looked over your code and I see a syntax error.
To solve this syntax error look at how you declared your cashRegister properties, it should basically be the same set-up with declaring a method which is, propertyName: value,
Just a vague hint that can help solve your syntax error.
Another hint would be asking yourself what are the methods in my cashRegister object? Are they declared the same way?

Hope this helps..:slightly_smiling: