Help! 7. You Deserved It! (Console shows $13.74 but false pops up)


#1



7. You Deserved It!


Help! The console shows $13.74 which I calculated to be correct if a 20% discount is applied to the total, but this message keeps popping up: "Oops, try again. false"

Could someone tell me where i went wrong?


function StaffMember(name,discountPercent){
    this.name = name;
    this.discountPercent = discountPercent;
}

var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);

// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember ("Colin",20);


var cashRegister = {
    total:0,
    lastTransactionAmount: 0,
    add: function(itemCost){
        this.total += (itemCost || 0);
        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;
    },
    voidLastTransaction : function(){
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
    },
    // Create a new method applyStaffDiscount here
   appplyStaffDiscount: function(employee){
        this.total*=(1-employee.discountPercent/100)
    }
    
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object 
// to applyStaffDiscount
cashRegister.appplyStaffDiscount(me)


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


#2

This is an SCT error, and not a script error, per se. We need to examine the minutae to solve it.


#3

Parens are needed.

As I see this, at present, the only thing that is not asked for is .toFixed(), at least not to my memory. Don't know what lesson this is since there's no link, so I'm only guessing. Is 13.74 the expected amount? Or a float of some kind? Check the instructions for every little detail. In the meantime, please post a link to the exercise so we can have a look. Thank you.


#4

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

That's the link of the exercise. Sorry, I'm really new to JS and to this website and forum. I assumed people would know when I included the title.

Also, the .toFixed() part was included in the code when I started the exercise.


#5

While we might 'know', we don't have the exercise on speed dial.

So it was introduced, with no explanation. This is a cause for concern


#6

I am having the exact same problem, only the console shows "Your bill is 17.18." The error message says "false."

Could someone examine my code and tell me what's wrong?

function StaffMember(name,discountPercent){
    this.name = name;
    this.discountPercent = discountPercent;
}

var sally = new StaffMember("Sally",5);
var bob = new StaffMember("Bob",10);

// Create yourself again as 'me' with a staff discount of 20%
var me = new StaffMember("Amzaloopich",20);

var cashRegister = {
    total:0,
    lastTransactionAmount: 0,
    add: function(itemCost){
        this.total += (itemCost || 0);
        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;
    },
    voidLastTransaction : function(){
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
    },
    // Create a new method applyStaffDiscount here
    applyStaffDiscount: function(employee) {
        return (StaffMember.discountPercent / 100) * this.total;
    }
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',3);
// Apply your staff discount by passing the 'me' object 
// to applyStaffDiscount
cashRegister.applyStaffDiscount(me);

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

#7

The return value of the applyStaffDiscount method will be NaN since StaffMember.discountPercent is undefined. Fortunately this does not interrupt the program. The total that is displayed is before discount.

The method should not be returning anything, but instead making a direct adjustment to the total value.

 this.total *= 1 - employee.discountPercent / 100

#8

Thank you dude!

Your comments sure help me a lot; thx

epiccoolness


#9

Sorry I have another question.

Do you know what the "false" on the error message means? Thx if you can answer it. If you can't, that's okay.
:grin:


#10

It's the only response that the SCT has for the given error. Since you are getting a result in the console, there are no syntax errors.


#11

THX

:relieved: :sweat_smile:


#12

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