JS review Flashcards
when accessing an object’s property, do you need quotation marks when using bracket notation?
yes, if you are entering the actual property, and not another variable representing the property whose assigned value is already in quotes.
e.g.
function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)) {
return obj[checkProp];
} else {
return “not found”;
}
// Only change code above this line
}
console.log(checkObj({gift: “pony”, pet: “kitten”, bed: “sleigh”}, “gift”))
but you must use bracket, not dot notation when a string is more than one word.
testObj[‘an entree’];
from the following lesson:
// Setup
const testObj = {
“an entree”: “hamburger”,
“my side”: “veggies”,
“the drink”: “water”
};
// Only change code below this line
const entreeValue = testObj[‘an entree’]; // Change this line
const drinkValue = testObj[‘the drink’]; // Change this line
when using a VARIABLE to find the property value of obj, do you use bracket or dot?
use bracket
e.g.,
// Setup
const testObj = {
12: “Namath”,
16: “Montana”,
19: “Unitas”
};
// Only change code below this line
const playerNumber = 16; // Change this line
const player = testObj[playerNumber]; // Change this line
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables
how do you delete a property and it’s value from an object?
use the delete keyword, like this:
delete myDog.tails
from the lesson below:
// Setup
const myDog = {
“name”: “Happy Coder”,
“legs”: 4,
“tails”: 1,
“friends”: [“freeCodeCamp Campers”],
“bark”: “woof”
};
// Only change code below this line
delete myDog.tails
how can you test if an object has a certain property name?
the .hasOwnProperty(propname) method of objects to determine if that object has the given property name.
how do you remove items from an array (in the middle of the array)
use the .splice( ) method
e.g.
let array = [‘today’, ‘was’, ‘not’, ‘so’, ‘great’];
array.splice(2, 2);
returns:
[‘today’, ‘was’, ‘great’]
how do you add items to an array using splice?
splice can take MORE than two arguments. starting with the third argument (or more), all of these will be inserted where deletions were made using the first two arguments
e.g.
function htmlColorNames(arr) {
// Only change code below this line
arr.splice(0, 2, ‘DarkSalmon’, ‘BlanchedAlmond’)
// Only change code above this line
return arr;
}
console.log(htmlColorNames([‘DarkGoldenRod’, ‘WhiteSmoke’, ‘LavenderBlush’, ‘PaleTurquoise’, ‘FireBrick’]));
returns:
[ ‘DarkSalmon’,
‘BlanchedAlmond’,
‘LavenderBlush’,
‘PaleTurquoise’,
‘FireBrick’ ]
explain the slice () method. how is it different from splice?
The next method we will cover is slice(). Rather than modifying an array (like splice( )), slice() copies or extracts a given number of elements to a new array, leaving the array it is called upon untouched. slice() takes only 2 parameters — the first is the index at which to begin extraction, and the second is the index at which to stop extraction (extraction will occur up to, but not including the element at this index)
e.g.
function forecast(arr) {
// Only change code below this line
arr = arr.slice(2, 4)
return arr;
}
// Only change code above this line
console.log(forecast([‘cold’, ‘rainy’, ‘warm’, ‘sunny’, ‘cool’, ‘thunderstorms’]));
returns warm, sunny
What is the spread operateor and how does it work?
let thisArray = [true, true, undefined, false, null];
let thatArray = […thisArray];
thatArray equals [true, true, undefined, false, null]. thisArray remains unchanged and thatArray contains the same elements as thisArray.
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/copy-an-array-with-the-spread-operator
how could you combine arrays or insert one array into another?
Use the spread operator
That is: … arrayname
e.g.,
function spreadOut() {
let fragment = [‘to’, ‘code’];
let sentence = [‘learning’, …fragment, ‘is’, ‘fun’]; // Change this line
return sentence;
}
console.log(spreadOut());
returns:
[ ‘learning’, ‘to’, ‘code’, ‘is’, ‘fun’ ]
explain a for… in statement (it does loop but it’s called a statement)
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-the-keys-of-an-object-with-a-for—in-statement
e.g.
const users = {
Alan: {
online: false
},
Jeff: {
online: true
},
Sarah: {
online: false
}
}
function countOnline(usersObj) {
// Only change code below this line
let number = 0;
for (let user in usersObj) {
if (usersObj[user].online === true) {
number += 1;
}
}
return number;
// Only change code above this line
}
console.log(countOnline(users));
how do you get all the object keys?
use the Object.keys( ) method
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/generate-an-array-of-all-object-keys-with-object-keys
e.g.
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
function getArrayOfUsers(obj) {
// Only change code below this line
return Object.keys(obj)
// Only change code above this line
}
console.log(getArrayOfUsers(users));