JS101-Part 3 Flashcards
How to add a certain number of a certain character
String.prototype.repeat([number of repetitions]_
let numbers = [1, 2, 3];
numbers[6] = 5;
numbers[4];
What does the last line return?
What is the value of numbers[4]?
undefined
But it should be said it evaluates to undefined
The value is nothing, not even undefined
How can you determine whether a given string ends with an exclamation mark (!)?
let str1 = “Come over here!”; // true
let str2 = “What’s up, Doc?”; // false
str1.endsWith(“!”); // true
str2.endsWith(“!”); // false
How to check if an object has a property
Does let ages = { Herman: 32, Lily: 30, Grandpa: 402, Eddie: 10 } contain “Spot”?
Object.prototype.hasOwnProperty(Spot)
Determine whether the name Dino appears in the strings below (check each string separately)
let str1 = “Few things in life are as important as house training your pet dinosaur.”;
let str2 = “Fred and Wilma have a pet dinosaur named Dino.”;
using .match
What are 2 other ways to add an element to an array other than .push
.concat(element)
array[array.length] = element
Replace all instances of a substring in a string
‘a string’.replace(/regex/g, ‘REPLACEMENT’)
it’s the g tag to do all instances.
join two arrays [1,2,3] and [4,5,6]
.concat
Write a 1 line expression to count the number of occurrences of a substring. (2 ways)
let statement1 = “The Flintstones Rock!”;
let statement2 = “Easy come, easy go.”;
.match will with the g tag will return an array with all occurrences.
But will return null if none. Taking length of null throws error.
So do || []
console.log((statement2.match(/t/g) || []).length);
OOOORRRR
statement1.split(‘’).filter(char => char === ‘t’).length;
Write three different ways to remove all of the elements from the following array:
numbers.length = 0;
numbers.splice(0, numbers.length);
while (numbers.length) {
numbers.pop();
}
console.log([1, 2, 3] + [4, 5]);
1, 2, 34, 5
Return a new string that swaps the case of all of the letters:
let munstersDescription = “The Munsters are creepy and spooky.”;
if (char === char.toUpperCase())
etc.
Difference between
anArray.push(1, 2)
and
anArray.concat(1, 2)
.concat doesn’t mutate, and this will not have any effect if not:
anArray = anArray.concat(1, 2);
Will this mutate munsters:
let munsters = {
Herman: { age: 32, gender: “male” },
Lily: { age: 30, gender: “female” },
Grandpa: { age: 402, gender: “male” },
Eddie: { age: 10, gender: “male” },
Marilyn: { age: 23, gender: “female” }
};
function messWithDemographics(demoObject) {
Object.values(demoObject).forEach(familyMember => {
familyMember[“age”] += 42;
familyMember[“gender”] = “other”;
});
}
yes,
the Object.values makes a copy, but copies references to the deeper objects.
Are these the same?
function first() {
return {
prop1: “hi there”
};
}
function second() {
return
{
prop1: “hi there”
};
}
console.log(first());
console.log(second());
no, second() returns undefined.
It assumes a semicolon.