JS101-Part 3 Flashcards

1
Q

How to add a certain number of a certain character

A

String.prototype.repeat([number of repetitions]_

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

let numbers = [1, 2, 3];
numbers[6] = 5;
numbers[4];

What does the last line return?
What is the value of numbers[4]?

A

undefined

But it should be said it evaluates to undefined

The value is nothing, not even undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

str1.endsWith(“!”); // true
str2.endsWith(“!”); // false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to check if an object has a property
Does let ages = { Herman: 32, Lily: 30, Grandpa: 402, Eddie: 10 } contain “Spot”?

A

Object.prototype.hasOwnProperty(Spot)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.”;

A

using .match

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are 2 other ways to add an element to an array other than .push

A

.concat(element)
array[array.length] = element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Replace all instances of a substring in a string

A

‘a string’.replace(/regex/g, ‘REPLACEMENT’)
it’s the g tag to do all instances.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

join two arrays [1,2,3] and [4,5,6]

A

.concat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.”;

A

.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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write three different ways to remove all of the elements from the following array:

A

numbers.length = 0;

numbers.splice(0, numbers.length);

while (numbers.length) {
numbers.pop();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

console.log([1, 2, 3] + [4, 5]);

A

1, 2, 34, 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Return a new string that swaps the case of all of the letters:
let munstersDescription = “The Munsters are creepy and spooky.”;

A

if (char === char.toUpperCase())
etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Difference between
anArray.push(1, 2)
and
anArray.concat(1, 2)

A

.concat doesn’t mutate, and this will not have any effect if not:
anArray = anArray.concat(1, 2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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”;
});
}

A

yes,
the Object.values makes a copy, but copies references to the deeper objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Are these the same?
function first() {
return {
prop1: “hi there”
};
}

function second() {
return
{
prop1: “hi there”
};
}

console.log(first());
console.log(second());

A

no, second() returns undefined.
It assumes a semicolon.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Does .concat() mutate on arrays?
What does it return?

A

Does not mutate
It returns a copy of the original array concatenated with the argument (even if it’s not an array.

17
Q

What is a literal?

A

Its the notation represetning a value in source code
Let a = 1
Let b = 2
Let c = a + b
1 and 2 are literals.
3 is not

18
Q

What is string interpolation?

A

interpolate variables and expressions into strings

19
Q

How to represent arraysthat are too long

A

[
“String1’,
“String2”,
]

20
Q

What is an expression?

A

Anything that js can evaluate to a value and be used in subsequent code

21
Q

How do you know what a statement returns?

A

Statements don’t really return anything. they will evaluate as undefined and not be usable in subsequent code.

Let x = ‘@soemthing’
Undefined

X = 67;
67

22
Q

Assignments and reassignments are expressions t or f
Of t what do they return?
How about declarations and initializations?

A

T
Return whatever evaluates right of =
Decalarations and Initialization with assignments return undefined

23
Q

Can you have a value not represented by a literal?

A

Yes,
a = 1
b = 2
c = a + b;

c has the value of 3 but no literal