L1: String Methods, for while loops Flashcards
What does String.prototype.concat() mutate/return?
Returns new string, basically the same as going caller + argument
Can .concat take multiple arguments
yes
basically argument + argument + argument
What does String.prototype.includes() mutate/return?
Returns boolean if the caller contains the substring
What does the optional second argument do in String.prototype.includes()
Indicates with index to start looking from
What does String.prototype.split() do if there’s no argument?
Just gives the full string in an array.
What does String.prototype.trim() mutate/return?
returns string with caller’s whitespaces, newlines \n, and tabs \t remove
method to only remove spaces from beginning or end
.trimStart()
.trimEnd()
Return boolean if a string contain a substring
.includes()
Return the character at a certain index of a string (2 ways)
.charAt(2)
or
‘string’[2]
What’s the difference between .charAt and ‘string’[2]in regard to whether the character exists
‘string’[2] will return undefined if that index doesn’t exist
.charAt will return an empty string.
Return the Unicode code point or character code for a given index of a string
‘abcdef’.charCodeAt(1)
Create a character for a given char code argument
String.fromCharCode(97)
Why are some methods prototypes and some not? like
String.fromCharCode(97)
‘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)
Return boolean if a string ends or starts with a certain substring
String.prototype.endsWith
String.prototype.startsWith
Return a string a number of times
String.prototype.repeat
Is a while loop or a for loop preferred for iterating ove rcollections?
for loop
Why would a for/in loop give a different result from a whlie loop (with Object.keys() ethod) for iterating over object keys?
for/in is that it iterates over the properties of an object’s prototypes as well:
When might you use:
while (true) {
if (str.length >= 10) {
break;
}
over
while (str.length < 10)
or do while?
When you want to break in the middle of a loop.
What is a guard clause
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.
Generally you should use blocks for if statements and not single lines. t or f?
Are there any exceptions
T
Single lines are ok for guard clauses, like
if (numbers[index] % 2 === 1) continue;
Instead of
if (numbers[index] % 2 === 1) {
continue;
}
Convert single digit string number ‘1’ to a number using charcode
‘123’.charCodeAt(2) - ‘0’.charCodeAt(0)
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?
.padStart()
.padEnd()
It trims the filler string. It always goes to the target length.
What do these returns if the index is outside the range of the string:
charAt, charCodeAt, ‘asdf’[6]
charAt- undefined
charCodeAt- undefined
‘asdf’[6]- undefined
What happens if string .includes uses a negative index for the second parameter
What about array.includes with negative inde second parameter
Seems to just search from the beginning with strings
With arrays, still searches forward, but does the whole -index + length