String Knowledge Flashcards

1
Q

Why does this evaluate true?

‘z’ > ‘a’

Why is this false?

‘tb’ < ‘ta’

A

So strings become NaN when converted to a number. But with some comparisons, JavaScript uses the so-called “dictionary” or “lexicographical” order. It does this letter by letter (sort of like a number).
so z has a higher number than a

tb has a higher number than ta

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

“hello my name is stringy the whooping string”
but we want a function that will capitalize the first letter of each word
“Hello My Name Is Stringy The Whooping String”

A

With .split(), .map(), charAt(), .toUpperCase() and slice(). join()

Take the string

.split() it

.map() that array taking each word and using charAt(0), toUpperCase() it, and concatentate it with the rest of the word sliced.

Then join it.

OR

.split(), .map(), x[0].toUpperCase + x.subString

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

What happens with this code:

alert( ‘hello ${1}’ );

A

// hello 1

is alerted in browser

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

Evaluate:

‘Bee’ > ‘Be’

A

True

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

‘z’ > ‘aaaa’

A

True

It judges character from left to right. Returns whenever oneis larger than the other. it does not convert all characters into a single value.

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

How does the string > or < algorithm work

A

Compare the first character of both strings.

If the first character from the first string is greater (or less) than the other string’s, then the first string is greater (or less) than the second. We’re done.

Otherwise, if both strings’ first characters are the same, compare the second characters the same way.

Repeat until the end of either string.

If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.

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

Two benefits of backticks

A
  1. Allow embedded expressions

2. Allow string to span multiple lines

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

escape a backslash

A

\\

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

Add a tab in a string

A

\t

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

Add a unicode symbol or character

A

\xXX Unicode character with the given hexadecimal Unicode XX, e.g. ‘\x7A’ is the same as ‘z’.

\uXXXX A Unicode symbol with the hex code XXXX in UTF-16 encoding, for instance \u00A9 – is a Unicode for the copyright symbol ©. It must be exactly 4 hex digits.

\u{X…XXXXXX} (1 to 6 hex characters) A Unicode symbol with the given UTF-32 encoding. Some rare characters are encoded with two Unicode symbols, taking 4 bytes. This way we can insert long codes.

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

What is alerted:

alert( ‘My\n’.length );

A
// 3
\n is a single “special” character, so the length is indeed 3.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
What is str at the end?
let str = 'Hi';
str[0] = 'h';
A

Hi

strings are immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
What is str at the end?
let str = 'Hi';
str = 'h' + str[1];
A

hi

the variable is reassigned to something. The string did not change.

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

‘a’ < ‘Z’
‘a’ < ‘b’
‘a’ < ‘A’

A

false
true
false

Here is the number assigned to each character ascending:
let str = '';
for (let i = 65; i <= 220; i++) {
  str += String.fromCodePoint(i);
}
alert( str );
Gives:
// ABCDEFGHIJKLMNOPQRSTU
VWXYZ[\]^_`abcdefghijklmn
opqrstuvwxyz{|}~€‚ƒ„
 ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉ
ÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜ
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

‘Österreich’ > ‘Zealand’

‘Osterreich’ > ‘Zealand’

A

true
false

let str = ‘’;

for (let i = 65; i <= 220; i++) {
  str += String.fromCodePoint(i);
}
alert( str );
// ABCDEFGHIJKLMNOPQRSTU
VWXYZ[\]^_`abcdefghijklmn
opqrstuvwxyz{|}~€‚ƒ„
// ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉ
ÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜ
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Count the number of instances of a substring in a string

A

myString.match(/substring/g).length