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 Complete #18

Open
wants to merge 1 commit 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
76 changes: 70 additions & 6 deletions exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,128 @@ 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 cat = {};
cat.name = name, //all in dot notation
cat.age = age,
cat.meow = function () {
return ('Meow!');
}; return cat;
}

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; // 47 in Notes -- bracket notation used, since actual key name is not known.
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](); // Only bracket notation will work for this.
}

function multiplyMysteryNumberByFive(mysteryNumberObject) {
// mysteryNumberObject has a property called mysteryNumber
// multiply the mysteryNumber property by 5 and return the product
return mysteryNumberObject.mysteryNumber*5; // Notice here that bracket notation does not work.
}

function deleteProperty(object, property) {
// remove the property from the object
// return the object
delete object[property]; //Only bracket notation works
return object;
}

function newUser(name, email, password) {
// create a new object with properties matching the arguments passed in.
// return the new object
var user = {};
user.name = name,
user.email = email,
user.password = password;
return user;
}

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]) { //make sure you don't forget the brackets otherwise you will get a 'Parsing error'
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; // WOW! Very simple!
return user;
}

function addFriend(user, newFriend) {
// user has a property called friends that is an array
// user has a property called friends that is an array -- means you must use bracket notation
// add newFriend to the end of the friends array
// return the user object
user.friends.push (newFriend);
return user; // Remember that you can only have one return within a function!

}

function setUsersToPremium(users) {
function setUsersToPremium(users) { // Just like line 108 from exercises.js homework2
// 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) {
// user has an array property called 'posts'
// posts is an array of post objects
// each post object has an integer property called 'likes'
// user has an array property called 'posts' ----------------- 'user.posts'
// posts is an array of post objects ------------------------- 'user.posts[i]'
// each post object has an integer property called 'likes' --- 'user.posts[i].likes'
// sum together the likes from all the post objects
// return the sum
var sum = 0;
for (var i = 0 ; i < user.posts.length ; i++) {
sum = sum + user.posts[i].likes; // Originally had user 'user.posts[post.likes[i]]' here. Know the difference!
} return sum;
}

// Is it possible to use the method below to achieve the same results?

/*

for (var key in user.posts) {
console.log('>>>key', key);
console.log('>>>value', user[key])

*/


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
Expand All @@ -85,6 +135,20 @@ function addCalculateDiscountPriceMethod(storeItem) {
// price -> 20
// discountPercentage -> .2
// discountPrice = 20 - (20 * .2)


storeItem.calculateDiscountPrice = function(/*price, discountPercentage*/) { // These arguments are not necessary...why?
//Is it because these arguments have been
//declared already in the storeItem object?

var discountPrice = this.price - (this.price*this.discountPercentage);

//var discount = (this.price)*(this.discountPercentage);
//var discountPrice = this.price - discount; //THIS (step-by-step approach) WORKS in place of the single line above too!


return discountPrice;
}; return storeItem;
}

// Do not modify code below this line.
Expand Down
14 changes: 14 additions & 0 deletions feynmanPrompts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Feynman Writing Prompts for Homework 3

Write out explanations of the following concepts like you are explaining it to a 12 year old. Doing this will help you quickly discover any holes in your understanding. Ask your questions on Slack.

##My Definitions:
---

1. `Objects`: Objects are a lot like what you interact with on facebook. Although you and your friends all have different names, emails, and passwords -- each of you has a value associated with each of those 'properties' that is specificaly associated with your FB profile. In Javascript, your FB profile can be compared to an 'Object', which contains different 'properties', such as you name, your email, and your password. Objects have the potential to store an infinite amount of information within each of these properties in the form of text, numbers, or booleans, not to mention arrays and even other objects!
2. `Properties`: See definition in `Objects`. Additionally, 'property' is somewhat ambiguously used to refer to the `key` specifically (the `key` being 'apples' in the following code: var object = {'apples': 5}) OR both the `key` and its associated `value` -- ('apples : 5').
3. `Methods`: Properties can include variables that represent functions, which, in the context of an `Object` are called `Methods`.
4. `for in loop`: Allows you to iterate over each of the `keys` in an `object`, conceptually in much the same way that we have used `for loops` to iterate over each of the `elements` in an `array` in previous assignments.
5. `Dot notation vs bracket notation`: There are two notations that you can use (both will work in the majority of cases) called dot notation and bracket notation. Nevertheless, dot notation is used the vast majority of time, except in instances where using it presents issues.
*One example of an instance like this is when you want to console.log a `value` (as opposed to its associated `key`) within a `for in loop`. In this case, you would need to use bracket notation rather than dot notation, as dot notation will return as undefined.
*You could use bracket notation ALL the time by replacing, for example, user.name with user ['name']; however, dot notation remains cleaner and thus, in addition to being visually less complicated, will likely reduce the occurance of typing errors.
Loading