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,
}
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.
- 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.
- An argument is the actual data we are using to feed into our function.
- We use them when we want to manipulate or access data in some way within out function.
savingsAmount
200
- 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)`
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.
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.
-
What would be a good function name for calculating the total amount invested in a given stock? calculateStockAmount
-
What would be a good name for the parameter that your function takes in? What does that parameter represent? companyName
-
What would be a good variable name for storing the data from the table above? totalAmount
-
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 }
-
Write a function that does the thing!
`function calculateStockAmount(companyName) { var totalAmount = companyName.price * companyName.shares return totalAmount }
calculateStockAmount(companyName)`
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