New String Methods Flashcards

1
Q

What method allows you to see if a string begins with a certain string?

A
var string = 'RFB2';
string.startsWith('RFB')
> true!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Would startWidth allow for case sensitivity?

A

No

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

What does the second param of startsWith allow you to do?

A
Starts after X characters
const string = 'RFB2';
string.startsWith('RFB', 3)
> true!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What method allows you to see if a string ends with a certain string?

A
const string = '2-AC-2018-JZ';
string.endsWith('JZ')
> true!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would you selectively use endsWith to ignore the last few characters of a string

A

Use the second parameter to choose how many characters from the start of the string you want to check:

const string = ‘ADGDRT001’;

string.endsWidth(‘RT’,6);

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

What method would you use to check for the presence of ‘ac’ in a string?

A

.includes(‘ac’);

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

What method allows you to repeat a string?

A

.repeat(‘string’);

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

What would .repeat() be useful. Give an example.

A

to format text in a left pad function.

function leftPad(str, length = 20) {
return `- ${ ' '.repeat(length - str.length) }`;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly