L1: String Methods, for while loops Flashcards

1
Q

What does String.prototype.concat() mutate/return?

A

Returns new string, basically the same as going caller + argument

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

Can .concat take multiple arguments

A

yes
basically argument + argument + argument

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

What does String.prototype.includes() mutate/return?

A

Returns boolean if the caller contains the substring

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

What does the optional second argument do in String.prototype.includes()

A

Indicates with index to start looking from

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

What does String.prototype.split() do if there’s no argument?

A

Just gives the full string in an array.

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

What does String.prototype.trim() mutate/return?

A

returns string with caller’s whitespaces, newlines \n, and tabs \t remove

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

method to only remove spaces from beginning or end

A

.trimStart()
.trimEnd()

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

Return boolean if a string contain a substring

A

.includes()

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

Return the character at a certain index of a string (2 ways)

A

.charAt(2)
or
‘string’[2]

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

What’s the difference between .charAt and ‘string’[2]in regard to whether the character exists

A

‘string’[2] will return undefined if that index doesn’t exist
.charAt will return an empty string.

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

Return the Unicode code point or character code for a given index of a string

A

‘abcdef’.charCodeAt(1)

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

Create a character for a given char code argument

A

String.fromCharCode(97)

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

Why are some methods prototypes and some not? like
String.fromCharCode(97)
‘a’

A

It’s a common pattern in different languages to write methods that don’t pertain to a specific value of a type directly on the class/constructor for that type. In this case, fromCharCode isn’t an operation you’d perform on a string value. That is, something like the following doesn’t make sense:
‘abcd’.fromCharCode(97)

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

Return boolean if a string ends or starts with a certain substring

A

String.prototype.endsWith
String.prototype.startsWith

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

Return a string a number of times

A

String.prototype.repeat

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

Is a while loop or a for loop preferred for iterating ove rcollections?

A

for loop

17
Q

Why would a for/in loop give a different result from a whlie loop (with Object.keys() ethod) for iterating over object keys?

A

for/in is that it iterates over the properties of an object’s prototypes as well:

18
Q

When might you use:
while (true) {
if (str.length >= 10) {
break;
}

over
while (str.length < 10)
or do while?

A

When you want to break in the middle of a loop.

19
Q

What is a guard clause

A

conditional statement that protects the body of a loop or function from having to deal with values it doesn’t need to handle.
Generally continue, break, or return.

20
Q

Generally you should use blocks for if statements and not single lines. t or f?
Are there any exceptions

A

T
Single lines are ok for guard clauses, like
if (numbers[index] % 2 === 1) continue;

Instead of
if (numbers[index] % 2 === 1) {
continue;
}

21
Q

Convert single digit string number ‘1’ to a number using charcode

A

‘123’.charCodeAt(2) - ‘0’.charCodeAt(0)

22
Q

Fill the start or end of a string with a certain string to a certain string length.
What happens if you try to fill with a string that is longer than the specified remaining string length?

A

.padStart()
.padEnd()

It trims the filler string. It always goes to the target length.

23
Q

What do these returns if the index is outside the range of the string:
charAt, charCodeAt, ‘asdf’[6]

A

charAt- undefined
charCodeAt- undefined
‘asdf’[6]- undefined

24
Q

What happens if string .includes uses a negative index for the second parameter

What about array.includes with negative inde second parameter

A

Seems to just search from the beginning with strings

With arrays, still searches forward, but does the whole -index + length