Short term memory --- "Oopss" message counts too many scans


#1



https://www.codecademy.com/en/courses/close-the-super-makert/0/3?curriculum_id=506324b3a7dffd00020bf661

Short term memory, no syntax errors, but all possible items scanned again
after printing out total!

So I get an "Oops message because one too many eggs, magazines and chocolates are counted as scanned" --- does my code somehow manage to show up a bug in the codeacademy analysis code?

Very odd behaviour, doesn't happen when I run it through rhino

Code is


var cashRegister = {
    total: 0,
//insert the add method here   

     add: function(cost){
        this.total+=cost;
        console.log("In add function");
      },    //DONT FORGET THE COMMA!
    
     scan: function (item) {
        switch (item) { 
        case "eggs": 
            this.add(0.98); 
                        console.log("Added " +item);
            break;
        case "milk": 
            this.add(1.23); 
                        console.log("Added " +item);
            break;
        case "magazine": 
            this.add(4.99); 
                        console.log("Added " +item);
            break;
        case "chocolate": 
            this.add(0.45); 
                        console.log("Added " +item);
            break;
        default:
            console.log("Item not found in price list --- not adding!");
        }   //end switch  
        
         console.log("Item scanned  was", item);
        return true; 

        } //end scan function

};  //end constructor


var retval=false;
console.log("scans begin");
retval=cashRegister.scan("eggs");
console.log('running total is '+cashRegister.total);
retval=cashRegister.scan("magazine");
console.log('running total  '+cashRegister.total);
retval=cashRegister.scan("chocolate");
console.log('running total  '+cashRegister.total);
console.log("scans end");

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


#2

Oh dear, I misread the error message "Have you" as "You Have", and misremembered the items to scan.

However, if I generate the "Oops" message, it still calls the add function for each possible item
after all the all calls to scan are completed --- which confused matters even more...


#3

Hi seagods, is your progblem solved? If not, please explain it clearly. Thanks!


#4

So,my end of the problem is solved, all I had to do was put in the right items to scan. The most confusing bit though was that the codeacademy software scans each item after the 'Your bill is' statement for each case in the switch. So I seem to have something to throw the codeacadamy analysis code a bit?


#5

Maybe this is related?