From 436c72a644799eb951ee611f44bd75de2d5b9279 Mon Sep 17 00:00:00 2001 From: sam-crabtree Date: Fri, 21 Jul 2017 21:51:31 -0500 Subject: [PATCH] Completed homework --- exercises.js | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/exercises.js b/exercises.js index be10a41..f5ab23a 100644 --- a/exercises.js +++ b/exercises.js @@ -7,61 +7,103 @@ function makeCat(name, age) { //add an age property to the object with the value set to the age argument //add a method called meow that returns the string 'Meow!' //return the object + var kitty = { + name: name, + age: age, + meow: function() { + var sayMeow = 'Meow!'; + return sayMeow; + } + }; + return kitty; } function addProperty(object, property) { //add the property to the object with a value of null //return the object //note: the property name is NOT 'property'. The name is the value of the argument called property (a string) + object[property] = null; + return object; } + function invokeMethod(object, method) { //method is a string that contains the name of a method on the object //invoke this method //nothing needs to be returned + object[method]( ); } function multiplyMysteryNumberByFive(mysteryNumberObject) { //mysteryNumberObject has a property called mysteryNumber //multiply the mysteryNumber property by 5 and return the product + var product = mysteryNumberObject.mysteryNumber * 5; + return product; } function deleteProperty(object, property) { //remove the property from the object //return the object + delete object[property]; + return object; } function newUser(name, email, password) { //create a new object with properties matching the arguments passed in. //return the new object + var newUserAccountInfo = { + name: name, + email: email, + password: password + }; + return newUserAccountInfo; } function hasEmail(user) { //return true if the user has a value for the property 'email' //otherwise return false + if (user.email !== undefined && user.email.length > 0) { + return true; + } else { + return false; + } } function hasProperty(object, property) { //return true if the object has the value of the property argument //property is a string //otherwise return false + if (object[property] !== undefined && object[property] === true) { + return true; + } else { + return false; + } } function verifyPassword(user, password) { //check to see if the provided password matches the password property on the user object //return true if they match //otherwise return false + if (password === user.password) { + return true; + } else { + return false; + } } function updatePassword(user, newPassword) { //replace the existing password on the user object with the value of newPassword //return the object + user.password = newPassword; + return user; } function addFriend(user, newFriend) { //user has a property called friends that is an array //add newFriend to the end of the friends array //return the user object + user.friends.push(newFriend); + return user; } function setUsersToPremium(users) { @@ -69,6 +111,10 @@ function setUsersToPremium(users) { //each user object has the property 'isPremium' //set each user's isPremium property to true //return the users array + for (var i = 0; i < users.length; i++) { + users[i].isPremium = true; + } + return users; } function sumUserPostLikes(user) { @@ -77,6 +123,14 @@ function sumUserPostLikes(user) { //each post object has an integer property called 'likes' //sum together the likes from all the post objects //return the sum + var likeSum = 0; + var countVariable = user.posts; + + for (var i = 0; i < countVariable.length; i++) { + likeSum = likeSum + countVariable[i].likes; + } + + return likeSum; } function addCalculateDiscountPriceMethod(storeItem) { @@ -87,6 +141,11 @@ function addCalculateDiscountPriceMethod(storeItem) { //price -> 20 //discountPercentage -> .2 //discountPrice = 20 - (20 * .2) + storeItem.calculateDiscountPrice = function() { + var discount = this.price - (this.price * this.discountPercentage); + return discount; + }; + return storeItem; } //Do not modify code below this line.