Question

I'm learning codeacademy javascript codes and I really not understand why it gives me this error:

TypeError: Unable to get property 'magazine' of undefined or null reference

  1. Write the add method which has a single parameter, itemCost. It will add the itemCost to the total.
  2. We have partially written the scan method for you and started a switch statement. Add the following 2 items to the switch statement:
    • "magazine", 4.99
    • "chocolate", 0.45

Finally, use the scan method to buy 2 eggs and 3 magazines.

var cashRegister = {
    total: 0,

    add: function(itemCost) {
        itemCost += 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);
                break;
        }
        return true;
    }
};


cashRegister.sacan['eggs', 'eggs', 'magazine', 'magazine']

console.log('Your bill is ' + cashRegister.total);
Was it helpful?

Solution

Looks like you got most of the bits right, but you've got a typo and are using the scan function incorrectly in a few ways.

Functions in Javascript are caleld with parentheses, not brackets as you have them. Additionally, the scan() function takes a single item as a parameter, not an array (or multiple parameters).

So to scan one 'eggs', the code would look like this:

cashRegister.scan('eggs');

That should get you back on track.

OTHER TIPS

Your code has many errors. If you want to get total value then try the following code.

This is a good place to start learning javascript.

<script>
var cashRegister = {
    total: 0,

    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;
        }
    }
};
cashRegister.scan('eggs');
cashRegister.scan('eggs');
cashRegister.scan('magazine');
cashRegister.scan('magazine');

alert('Your bill is ' + cashRegister.total);

</script>

This worked for me:

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top