object
objects are collections of properties
properties are a key-value pair
rather than accessing data using an index, we can custom keys.
const fitBitData ={
totalSteps :20200,
totalMiles: 211,
name:"mick"
};
Object access
fitBitData["totalSteps"]
fitBitData.totalSteps
objects can in array and array can in object
const comments = [
{username:'tammy',text:'lol'},
{username:'amy', text:'hi'}
]
comments[1].text //hi
Loop
repeat code
for loop
while loop
for ...of loop
for ...in loop
for (let i =1;i<= 10; i++){
console.log(i);
}//pay attention to let
for(let i = 1; i<=10; i+=2)
infinite loops
looping over arrays
const animals = ['lions', 'tigers', 'bears'];
for (let i = 0; i< animals.length; i++){
console.log(i, animals[i]);
}
// 0 'lions'
// 1 'tigers'
// 2 'bears'
for (let i = animals.length-1; i>=0; i--){
console.log(animals[i])
}
for (variable of iterable){
}
const subreddits =['books', 'pics','soccer']
for (let sub of subreddits){
console.log(`Visit reddit.com/r/${sub}')
}
for (let person in testSores)// access key
const testScores = {
keenan: 80,
damon:67,
kim:89
}
for (let person in testScores){
console.log(`${person} scored ${testScores[person]});
}
let total = 0;
let scores = Object.values(testScores);
for (let score of scores) {
total += score;
}
console.log(total/ scores.length)