What is the difference between var, let, and const?
Consider the following blocks of code.
let array = [];
for (var i = 0; i < 10; i++) {
array[i] = function () {
console.log('You clicked: ' + i);
};
}
array[5]() // what will log here?
let array = [];
for (let i = 0; i < 10; i++) {
array[i] = function () {
console.log('You clicked: ' + i);
};
}
array[5]() // what will log here?
- Explain what is being logged
- Explain why it’s being logged
- Explain the difference between block and function scope.
What is the typeof the first parameter passed to forEach, map, find, filter, and reduce? What is this first parameter typically called?
Explain the differences between object bracket notation and dot nation. When would brackets be better to use than dot?
Write a function that will take this array of objects and return an object that counts up all the groceryList
items
var array = [
{
name: 'yung-jhun',
groceryList: [
'grapes',
'avocados',
'chicken',
'tortillas',
'rice',
'pop-tarts'
]
},
{
name: 'alter-nate',
groceryList: [
'chips',
'kale',
'chicken',
'tortillas',
'yogurt',
'granola'
]
},
{
name: 'limbonator',
groceryList: [
'chips',
'eggs',
'tomatoes',
'tortillas',
'rice',
'avocados',
'pop-tarts'
]
}
]
=> {
grapes: 1,
avocados: 2,
chicken: 2,
tortillas: 3,
rice: 2,
'pop-tarts': 3,
tomatoes: 2,
chips: 2,
kale: 1,
yogurt: 1,
granola: 1,
eggs: 1
}
Consider the following code.
function Hoodlum1 (name, alterEgo) {
this.name = name;
this.secret = {
name: alterEgo,
getName: function () {
return this.name;
}
}
}
function Hoodlum2 (name, alterEgo) {
this.name = name;
this.secret = {
name: alterEgo,
getName: () => {
return this.name;
}
}
}
Answer the following questions:
- In Hoodlum1 when you call
getName
function what is being logged and why? - In Hoodlum2 when you call
getName
function what is being logged and why? - In Hoodlum1 how can I get the
getName
function to log thealterEgo
value?
Write a function that takes this large object and returns a new object with two keys yongerThan15
&& olderThan15
. The interaction should look like this:
var objects = {
person1: {name: 'taylor', age: 11},
person2: {name: 'jhun', age: 12},
person3: {name: 'alter-nate', age: 16},
person4: {name: 'meeka', age: 16}
}
// return an object that looks like this
{
youngerThan15: [
{name: 'taylor', age: 11},
{name: 'jhun', age: 12}
],
olderThan15: [
{name: 'alter-nate', age: 16},
{name: 'meeka', age: 16}
]
}