4. I have to scan it more than once


#1

I was able to pass without the total being displayed. Any idea why? Here is my code...

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
calc: function(numItems){
this.numItems = num;
},
scan: function(item, numItems) {

    switch (item, num) {
    case "eggs": this.add(0.98) * this.calc(numItems); break;
    case "milk": this.add(1.23) * this.calc(numItems); break;
    case "magazine": this.add(4.99) * this.calc(numitems); break;
    case "chocolate": this.add(0.45) * this.calc(numItems); break;
    }

}

};

// scan each item 4 times
cashRegister.scan("eggs", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("chocolate", 4);

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


#2

I had a similar issue. When the parameter 'quantity' is included in the 'scan' function, the program prints to the console properly but the exercise does not pass on to the next one. The error message "Oops, try again. It doesn't seem like your 'quantity' actually works!" is displayed.

Conversely, if the 'quantity' parameter is left out, the the program does not print properly (ReferenceError: quantity is not defined) but I am allowed to pass on to the next exercise.

This code is allowed to pass on to the next exercise:

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item) {
switch (item) {
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.9 * quantity); break;
case "chocolate": this.add(0.45 * quantity); break;
}
}
};

// scan each item 4 times
cashRegister.scan("eggs", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("chocolate", 4);

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

This is not allowed to pass on to the next exercise:

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item, quantity) {
switch (item) {
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.9 * quantity); break;
case "chocolate": this.add(0.45 * quantity); break;
}
}
};

// scan each item 4 times
cashRegister.scan("eggs", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("chocolate", 4);

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

//I am not changing anything from one code set to the next except removing and including the 'quantity' parameter in the scan function.


#3

Thanks for the insight. I realize my syntax is correct but not exactly what the console is expecting.


#4

i'm not understanding why you are not supposed to add 'quantity' to the scan ( ). Logically, doesn't it have to have two parameters ?


#5

Because it is a switch and it does not take params but a single expression.

I am no authority in this but I think it is function is to handle simple things. You could probably but switch into a function with multiple params.