GLITCH/BUG (Just RUN this really quick)


#1

SO! i did 'I Have To Scan It More Than Once' and it makes me do a function. I made it wrong -
it should give me an error - some things are 100% wrong... and yet it works...

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

// scan each item 4 times
cashregister.scan("eggs" , 4)


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

here are the problems if you're curious

  scan: function(item , quantity) {
        for (var a = 0; a <= ammount; a++)

the function here takes quantity - - but in my 'for' statement - - it uses ammount... so it should infinite loop/ break because ammount isn't defined.

cashregister.scan("eggs" , 4)

^^ doesn't work because the R in cashRegister isn't capitalized & therefor it should say 'cashregister is not defined' but... it works!? can someone explain... or is this a bug?


#2

There won't be an infinite loop because the variable is undefined. That will throw an error.

What's more, I'm not sure that loop is in the instructions, and fail to see what purpose it serves. Why run through the switch multiple times when we can simply multiply?

this.add(1.23 * quantity)

#3

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