Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 3 #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,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 obj = {
name: name,
age: age,
meow: function() {
return 'Meow!';
}
};
return obj;
}

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
return mysteryNumberObject.mysteryNumber * 5;
}

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 newObj = {
name: name,
email: email,
password: password
};
return newObj;
}

function hasEmail(user) {
// return true if the user has a value for the property 'email'
// otherwise return false

if (user.email) {
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]) {
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
return (user.password === password);
}

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) {
// users is an array of user objects.
// 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) {
Expand All @@ -75,16 +113,28 @@ 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 counterr = 0;
for (var i = 0; i < user.posts.length; i ++) {
counterr += user.posts[i].likes;
}
return counterr;

}

function addCalculateDiscountPriceMethod(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
// example:
// example:
// price -> 20
// discountPercentage -> .2
// discountPrice = 20 - (20 * .2)
storeItem.calculateDiscountPrice = function () {
var discount = storeItem.price * storeItem.discountPercentage;
var discountPrice = storeItem.price - discount;
return discountPrice;
};
return storeItem;
}

// Do not modify code below this line.
Expand Down
9 changes: 9 additions & 0 deletions explanations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1. In Javascript an object is a collection of properties. An object allows code to mimic the qualities of things in the world. A variable only has one value at a time. An object can have many properties at once, just as things in the real world do. A car, for instance, has a color, length, age, year/make/model, identification number, etc.

2. A property is an association between a name (a key) and a value.

3. A method is an object property whose value is a function. Methods are especially useful for adding, modifying, or retrieving object data.

4. A for in loop in JS iterates over the enumerable properties of an object, either those specifically defined for the object, or those enumerable ones inherited from object prototypes. It can be used to iterate over an array (as indexes are just names of array properties), in cases when the output order doesn't matter.

5. Dot notation uses a period to separate the reference to an object from the reference to its property: `let sound = obj.cat;`. Bracket notation uses brackets to do the same thing: `let sound = obj['cat'];`.
Loading