diff --git a/Explanations.txt b/Explanations.txt new file mode 100644 index 0000000..f354034 --- /dev/null +++ b/Explanations.txt @@ -0,0 +1,33 @@ +Objects: Objects are a way to hold and categorize information. They are a way +to collect properties. Each property can either be an association between a key +and a value, or a method. + +---------------------------------------------------------- + +Properties: A property is a way to describe something. For example, if you have a +"car" object, you may want to have a property that describes the car's color or how +many doors it has. Also, you may have another property that describes something the +car does. For example, you could have a function that describes the sequence of events +as the car is started (car.pressBrake(), car.turnKey(), car.startEngine()). + +---------------------------------------------------------- + +Methods: A method is an action (or a collection of actions) grouped under a single function +to complete a task. For example, you could have a method called sum() that takes two parameters and +adds them. + +---------------------------------------------------------- + +for in loop: A for in loop is a way to iterate over values and perform an action. You could +iterate over array values and add five to each odd number, or iterate over each property in +an object. It is a very versatile way to efficiently perform a large action set without +using verbose language. + + +---------------------------------------------------------- + +dot notation vs bracket notation: In some cases, dot notation and bracket notation are +equivalent, such as using console.log vs console[log]. When a variable is unknown, such as +on a webpage where a user interacts by entering variables into the field linked to a method, +it is often better to use bracket notation because it will consider the value, as opposed to +the literal string entered. \ No newline at end of file diff --git a/exercises.js b/exercises.js index be10a41..358dc48 100644 --- a/exercises.js +++ b/exercises.js @@ -7,64 +7,106 @@ 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 newCat = { + + name: name, + age: age, + meow: function () { + return 'Meow!'; + } + }; +return newCat; } function addProperty(object, property) { + object[property] = null; + return object; //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) } 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 } function deleteProperty(object, property) { + delete object[property]; + return object; //remove the property from the object //return the object } function newUser(name, email, password) { + var newObject = { + name: name, + email: email, + password: password + }; + return newObject; //create a new object with properties matching the arguments passed in. //return the new object } function hasEmail(user) { + if (user.email !== undefined && user.email.length > 0) { + return true; + } + return false; //return true if the user has a value for the property 'email' //otherwise return false } function hasProperty(object, property) { + if (object[property] !== undefined) { + return true; + } 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; + } + 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 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' //set each user's isPremium property to true @@ -72,6 +114,14 @@ function setUsersToPremium(users) { } function sumUserPostLikes(user) { + + var sum = 0; + var userPosts = user.posts; + for (var i = 0; i < userPosts.length; i++) { + sum += userPosts[i].likes; + } + return sum; + //user has an array property called 'posts' //posts is an array of post objects //each post object has an integer property called 'likes' @@ -80,6 +130,12 @@ function sumUserPostLikes(user) { } function addCalculateDiscountPriceMethod(storeItem) { + + storeItem.calculateDiscountPrice = function() { + var discount = this.price - (this.price * this.discountPercentage); + return discount; + }; + return storeItem; //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