Skip to content

Instantly share code, notes, and snippets.

@Leasw144
Created June 15, 2020 01:27
Show Gist options
  • Save Leasw144/e2f492d85c5d3f2c0ab2a666f10b6706 to your computer and use it in GitHub Desktop.
Save Leasw144/e2f492d85c5d3f2c0ab2a666f10b6706 to your computer and use it in GitHub Desktop.
MOD 2 Pre-work

5. Practice

Take the following sentence and create an object of key/value pairs that represent the information presented:

I have a car that is a black Jeep with 4 wheels and can drive, stop, and honk.

var car = { color: black, type: Jeep, wheels: 4, canDrive: true, canStop: true, canHonk: true, }

Now, take the following object and turn it into an english sentence:

let student = { name: ‘Bob Loblaw’, module: 2, study() { console.log(‘Studying hard!’); } }

Bob Loblaw is a student in module 2 that is capable of studying hard.

Parameters vs Arguments

  1. A parameter is a placeholder of data that we can place into a function or method. The data that is eventually fed in will eventually be a manipulated or accessed in some way.
  2. An argument is the actual data we are using to feed into our function.
  3. We use them when we want to manipulate or access data in some way within out function.

In the following code example:

What is the name of the parameter?

savingsAmount

What is the value of the argument?

200

  1. You are building a tip calculator for patrons to use at a restaurant. Write a function that will take in a dollar amount as a number and return 20% of that number. Your first patrons have a total bill of 200. Invoke your tip calculator function using that bill. (Pay close attention to your naming conventions!)

`function calculateTip(totalBill) { var tip = totalBill * .20 return tip }

calculateTip(200)`

Creating and Accessing Values in Arrays & Objects

let kittens = [ { name: ‘George’, age: 3, breed: ‘tabby’ }, { name: Bob, age: 2, breed: ‘siamese’ }, { name: Camila, age: 1, breed: ‘orange’ }, { name: Josia, age: 1, breed: ‘snow’ } ];

a. How would we access the first kitten in the array? kittens[0]

b. When accessing an element in an array like kittens[3], what is this syntax called? What does the number 3 represent?

bracket notation. 3 represents what index number the object is.

Given the object below:

let kitten = { name: ‘George’, age: 3, breed: ‘tabby’ }; C. How would we add a key/value pair to this object that tells us the kitten’s owner is Brittany? kitten.owner = 'Brittany'

D. How could we access the name of our kitten using dot notation? kitten.name

E. How could we access the name of our kitten using bracket notation? kitten["name"]

F. If we were to console.log() kitten[‘breed’], what would be logged? string of tabby

G. If we were to run the following code, what would the function return? You would get the number 3.

Working with Data

  1. What would be a good function name for calculating the total amount invested in a given stock? calculateStockAmount

  2. What would be a good name for the parameter that your function takes in? What does that parameter represent? companyName

  3. What would be a good variable name for storing the data from the table above? totalAmount

  4. How might you structure the data from the table above into a format that will most easily allow you to write this function? Would an object be best? An array? Why? (You may want to brainstorm a couple different options and reflect on the pros and cons of each.) var companyName = { name: amazon, price: 400, shares: 10 }

  5. Write a function that does the thing!

`function calculateStockAmount(companyName) { var totalAmount = companyName.price * companyName.shares return totalAmount }

calculateStockAmount(companyName)`

Iterating Patterns

a. Describe what i represents what iteration of the loop we are in

b. Describe what var i = 0; says about our for loop it tells are for loop to start at index 0

c. Describe what i < 5; says about our for loop this loop will go over 5 times if it starts at 0

d. Describe what i++; says about our for loop tells our loop to increase by one after completion of each loop

e. What will get logged on the third iteration of this for loop? Why? string of pig because it is the third element in our array and thus on the third iteration

f. What will get logged on the final iteration of this for loop? Why? undefined because there are no other listed elements in our array that goes that far

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment