Done with Codecademy Javascript course!

Hooray! I finished up Codecademy’s Javascript course! Here’s the code of the very last lesson, the problems of which I am so proud of having solved. If you plan on taking this course, you miiiiight want to skip this one : )

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

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

var cashRegister = {
    total:0,
    lastTransactionAmount: 0,
    add: function(itemCost){
        this.total += (itemCost || 0);
        this.lastTransactionAmount = itemCost; // only figured out where to put this stmt w/ help
    },
    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) {
        this.total = this.total - (this.total * (employee.discountPercent / 100)); // mmm math
    }
    
};

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)); // `toFixed(2)` appears to limit the
                                                            // number of decimals of the output to 2.

Output:

$ Your bill is 13.744

Finally, an example of a class complex enough for me to see how it’s really used! As my friend Jules said, codecademy is a great way to get a snapshot of a language in action. Being fairly comfortable with Python, Javascript was an awesome class to take. I’ll be wrangling a wrap-up at some point soon, mostly to cover all the ways that an object can be created with/without methods, how they can be edited, and how they can instantiate. STAY TUNED FRIENDS

Advertisements

2 thoughts on “Done with Codecademy Javascript course!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s