This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by liorb
almost 11 years

Please check my code and tell me what's wrong i get 30.6!

var cashRegister = { total:0, add: function(itemCost){ this.total += itemCost; }, scan: function(item,quantity) { for (var i=1; i <= quantity ;i++) { 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); cashRegister.scan(“milk”,4); cashRegister.scan(“magazine”,4); cashRegister.scan(“chocolate”,4);

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

Answer 515e47e12902a54c81000c97

4 votes

Permalink

Instead of using a loop to add up your for the quantity, you could just add * quantity in your case lines. So the first one would be case "eggs": this.add(0.98 * quantity); break;.

points
Submitted by Anna Adam
almost 11 years

3 comments

liorb almost 11 years

Thanks :) but my way can work either

Michael Nail almost 11 years

While that’s true, doesn’t that break the DRY rule? “Don’t Repeat Yourself”?

I’m using a while look in this lesson, and I’m also getting 30.6. I feel foolish not using best practices just so that Codecademy can pass me :(

Yes, I WOULD like some French cries with my whine.

Jesse L. Pink almost 11 years

I tend to agree with these guys… sure, the (0.98 * quantity) may look prettier, and is probably more optimized, but using a for or while loop still gets the job done just fine, IMO, and should still pass.

Answer 516ea751276ac52186000c46

3 votes

Permalink

var cashRegister = {
total:0,
add: function(itemCost){
    this.total += itemCost;
},
scan: function(item,count) {
    switch (item) {
    case "eggs": this.add(0.98*count); break;
    case "milk": this.add(1.23*count); break;
    case "magazine": this.add(4.99*count); break;
    case "chocolate": this.add(0.45*count); 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);

points
Submitted by 划破的天空
almost 11 years

Answer 51790645fb2ac13b64001b19

0 votes

Permalink

assume Urs are right, what if someone just bought 4 eggs, 3 milk, 2 magazines and 1 chocolate… then here comes the loop problem ??

points
Submitted by crossoverer
almost 11 years