5/7 ("Bleep Bleep")


#1


5/7: Bleep Bleep


I'm all but sure that my code is right. However, the console spits out an almost correct answer, which makes the website says that I'm wrong. First of all, my code is below:


var cashRegister = {
	total:0,
	lastTransaction: 0,
	add: function(itemCost) {
		this.total += itemCost;
		this.lastTransaction = itemCost;
	},
	scan: function(item,quantity) {
		swift (item) {
		case “eggs”: this.add(0.98 * quantity); break;
		case “milk”: this.add(1.23 * quantity); break;
		case “magazine”: this.add(4.99 * quantity); break;
		case “chocolate”: this.add(0.45 * quantity); break;
		}
		return true;
	},
	voidLastTransaction: function() {
		this.total -= this.lastTransaction;
	}
};

cashRegister.scan(“eggs”,4);
cashRegister.scan(“milk”,4);
cashRegister.scan(“magazine”,4);
cashRegister.scan(“chocolate,4”);

cashRegister.voidLastTransaction();
cashRegister.scan(“chocolate”,3);

console.log(“Your bill is “ + cashRegister.total);


When I submit this, the console prints out "Your bill is 30.150000000000002" instead of the correct 30.15. This makes the website think that I'm wrong and says "Oops, try again. false". I would assume that I've made an error, but having an output that's only off by 2*10^-15 makes me wonder if something else is off this time. I'd appreciate any help.

PS - I used a console.log() throughout my code in an attempt to find the problem, and I've determined that the snag occur at the 2nd to last line:
cashRegister.scan("chocolate",3)
Before this line, everything works fine.


#2

Read this post.


#3

Good suggestion. It helped, but not entirely. The console puts out the correct, four-digit price now, but the website still responds with "Oops, try again. false". I've tried multiple times, even after refreshing the page, clearing my internet history, and cleaning out all of my computer's cookies.


#4

@envee10, try my code and compare with your:

var cashRegister = {
    total:0,
    //Dont forget to add your property
    lastTransactionAmount: 0,
    add: function(itemCost) {
        this.total +=  itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function() {
        this.total -= this.lastTransactionAmount;
    }   
    
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();
cashRegister.scan('chocolate', 3);


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

#5

Well, I'll be damned. It's seems like the website only agrees with you if you insert exactly 1 egg, 1 milk carton, 1 magazine, and 4 chocolates. If I use those specific quantity numbers for either your code or for mine, it says "Way to go!". Thanks for the help on that. I never would have played randomly picked those specific numbers.


#6

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