From 98f20c7457cc7ac51dbe3962e0a4fe2c9b8c4ff3 Mon Sep 17 00:00:00 2001 From: Marx Date: Fri, 17 Mar 2017 10:42:01 -0500 Subject: [PATCH 1/3] updated homework-3 --- exercises.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/exercises.js b/exercises.js index be10a41..7b6e11c 100644 --- a/exercises.js +++ b/exercises.js @@ -3,16 +3,25 @@ //Do not change any of the function names function makeCat(name, age) { + var cat = {}; + cat.name = name; + cat.age = age; + cat.meow = function (){ + return 'Meow!'; + }; //create a new object with a name property with the value set to the name argument //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 + return cat; } function addProperty(object, property) { + object[property] = null; //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) + return object; } function invokeMethod(object, method) { @@ -22,49 +31,80 @@ function invokeMethod(object, method) { } function multiplyMysteryNumberByFive(mysteryNumberObject) { + //mysteryNumberObject has a property called mysteryNumber //multiply the mysteryNumber property by 5 and return the product } function deleteProperty(object, property) { + delete object[property]; + return object; //remove the property from the object //return the object } function newUser(name, email, password) { + var user = {}; + user.name = name; + user.email = email; + user.password = password; + return user; //create a new object with properties matching the arguments passed in. //return the new object } function hasEmail(user) { +for (var x in user){ + if (x ==='email'){ + return true; +} else return false; +} //return true if the user has a value for the property 'email' //otherwise return false } function hasProperty(object, property) { + if (object.hasOwnProperty(property)){ + return true; + } else return false; //return true if the object has the value of the property argument //property is a string //otherwise return false } function verifyPassword(user, password) { + if (password === user.password){ + return true; + } else return false; //check to see if the provided password matches the password property on the user object //return true if they match //otherwise return false } function updatePassword(user, newPassword) { + user.password = newPassword; + return user; //replace the existing password on the user object with the value of newPassword //return the object } function addFriend(user, newFriend) { + user.friends.push(newFriend); + return user; //user has a property called friends that is an array //add newFriend to the end of the friends array //return the user object } function setUsersToPremium(users) { + + for (var e in users){ + if (e === 'isPremium'){ + e = true; + } + } + + return users; //users is an array of user objects. //each user object has the property 'isPremium' //set each user's isPremium property to true @@ -80,10 +120,16 @@ function sumUserPostLikes(user) { } function addCalculateDiscountPriceMethod(storeItem) { + calculateDiscountPrice = function(){ + var price; + var discountPercentage; + var discount = price * discountPercentage; + return price - discount; + }; //add a method to the storeItem object called 'calculateDiscountPrice' //this method should multiply the storeItem's 'price' and 'discountPercentage' to get the discount //the method then subtracts the discount from the price and returns the discounted price - //example: + //example: //price -> 20 //discountPercentage -> .2 //discountPrice = 20 - (20 * .2) From f286a4a3144064940638d7da44c144da1ad008ee Mon Sep 17 00:00:00 2001 From: Marx Date: Fri, 17 Mar 2017 21:32:05 -0500 Subject: [PATCH 2/3] update to homework answers --- exercises.js | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/exercises.js b/exercises.js index 7b6e11c..17f6bdc 100644 --- a/exercises.js +++ b/exercises.js @@ -25,13 +25,14 @@ function addProperty(object, property) { } function invokeMethod(object, method) { + object[method](); //method is a string that contains the name of a method on the object //invoke this method //nothing needs to be returned } function multiplyMysteryNumberByFive(mysteryNumberObject) { - + return mysteryNumberObject.mysteryNumber*5; //mysteryNumberObject has a property called mysteryNumber //multiply the mysteryNumber property by 5 and return the product } @@ -54,11 +55,10 @@ function newUser(name, email, password) { } function hasEmail(user) { -for (var x in user){ - if (x ==='email'){ + if (user.email){ return true; } else return false; -} + //return true if the user has a value for the property 'email' //otherwise return false } @@ -97,12 +97,8 @@ function addFriend(user, newFriend) { } function setUsersToPremium(users) { - - for (var e in users){ - if (e === 'isPremium'){ - e = true; - } - } +for (var i = 0; i < users.length; i++) + users[i].isPremium = true; return users; //users is an array of user objects. @@ -120,11 +116,9 @@ function sumUserPostLikes(user) { } function addCalculateDiscountPriceMethod(storeItem) { - calculateDiscountPrice = function(){ - var price; - var discountPercentage; - var discount = price * discountPercentage; - return price - discount; + storeItem.this.calculateDiscountPrice = function(){ + var discount = this.price * this.discountPercentage; + return this.price - discount; }; //add a method to the storeItem object called 'calculateDiscountPrice' //this method should multiply the storeItem's 'price' and 'discountPercentage' to get the discount From a59b9167fd57099486d4b992560457fbcac848b3 Mon Sep 17 00:00:00 2001 From: Marx Date: Sat, 18 Mar 2017 11:33:26 -0500 Subject: [PATCH 3/3] Final Updates to Homework-3 answers --- exercises.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/exercises.js b/exercises.js index 17f6bdc..6a54cf1 100644 --- a/exercises.js +++ b/exercises.js @@ -58,7 +58,6 @@ function hasEmail(user) { if (user.email){ return true; } else return false; - //return true if the user has a value for the property 'email' //otherwise return false } @@ -99,7 +98,6 @@ function addFriend(user, newFriend) { function setUsersToPremium(users) { for (var i = 0; i < users.length; i++) users[i].isPremium = true; - return users; //users is an array of user objects. //each user object has the property 'isPremium' @@ -108,6 +106,12 @@ for (var i = 0; i < users.length; i++) } function sumUserPostLikes(user) { + var postLikeSum = 0; + + user.posts.forEach(function(post){ + postLikeSum += post.likes; + }); + return postLikeSum; //user has an array property called 'posts' //posts is an array of post objects //each post object has an integer property called 'likes' @@ -116,10 +120,10 @@ function sumUserPostLikes(user) { } function addCalculateDiscountPriceMethod(storeItem) { - storeItem.this.calculateDiscountPrice = function(){ - var discount = this.price * this.discountPercentage; - return this.price - discount; - }; + storeItem.calculateDiscountPrice = function(){ + var discountPrice = this.price - (this.price * this.discountPercentage); + return discountPrice; +}; //add a method to the storeItem object called 'calculateDiscountPrice' //this method should multiply the storeItem's 'price' and 'discountPercentage' to get the discount //the method then subtracts the discount from the price and returns the discounted price @@ -127,6 +131,7 @@ function addCalculateDiscountPriceMethod(storeItem) { //price -> 20 //discountPercentage -> .2 //discountPrice = 20 - (20 * .2) +return storeItem; } //Do not modify code below this line.