Why can't I use *2 or *3 instead of writing out each item to scan? Short-Term Memory


#1

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

I get a "Have you scanned eggs twice and magazine three times?" error message and Your bill is 0 printed when I write the below code and I am wondering why. I know what the correct code is but I am wondering why I can't write:

cashRegister.scan("eggs"*2);
cashRegister.scan("magazine"*3);

Instead of writing out "eggs" twice and "magazine" three times.


Replace this line with your 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"*2);
cashRegister.scan("magazine"*3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);


#2

HI by "eggs" twice they mean that you should put this line twice

cashRegister.scan("eggs");

the same for "magazine"(magazine will be three time)


#3

I know, I was just frustrated that I couldn't make it easier and quicker. Then I went to the next exercise and it answered my question. :slight_smile:


#4

I know you have figured this out, but for anyone else wondering this:
Trying either

cashRegister.scan("eggs"*2);
or
cashRegister.scan("eggs")*2;
will not work because you are trying to multiple a string or trying to multiple a method, respectively, neither of which you can do (in JavaScript; as far as I know; however, I have found the attempts of others using Google to perform multiplication on strings). So in no way are you actually manipulating the cost of the item.

As a reference, you can refer to
JavaScript Operators


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.