This forum is now read-only. Please use our new forums! Go to forums
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
Answer 516ea751276ac52186000c46
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);
Answer 51790645fb2ac13b64001b19
assume Urs are right, what if someone just bought 4 eggs, 3 milk, 2 magazines and 1 chocolate… then here comes the loop problem ??
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
3 comments
Thanks :) but my way can work either
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.
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.