Strings and arrays Flashcards
Turn ‘apples, bananas, oranges, pears and salsa’ into an array but without ‘and salsa’
theString.split( ‘ ‘, 4);
What does someString.split(“”) do (first arg is empty string)?
Converts string to an array of characters. “abc” becomes [‘a’,’b’,’c’];
var arr = ["apple", "orange", "grape"]; var c = arr.push("banana"); what is c?
It returns the number 4 because push returns the length of the array, not the array.
var a = [a,b,c,d,e]. What does b = a.splice() do?
a will become an empty array and b will become [a,b,c,d,e]
someArray.splice(arg1, arg2); What is arg1 and arg2?
arg1 is the index to remove from; arg 2 is how many items after that.
How would you change [1,0,0,0,5] to [1, 2, 3, 4,5] using an array method?
theArray.splice( 1, 3, 2, 3 ,4);
What’s the best way to copy array myArr into newArr?
newArr = myArr.slice( ) or slice( 0 );
var a = "My String"; var b = a.slice(1); What happens to a and to b?
a = 'My String' b = 'y String'
How are substr(), substring(), and slice() similar/different?
The first arg in all is the starting index.
For substring and slice, arg2 is ending index.
For substr it’s the total characters/elements to take including starting index;
arr1 = [ 0, 2, 4];
arr2 = [ 1, 3, 5];
How could I change arr1 to be 0,2,4,1,3,5 ?
arr1 = arr1.concat(arr2); // concat is non-destructive.
what are the location methods?
indexof(arrayOrString, [startIndex]); // counts forward from startindex
lastIndexOf(arrayOrString, [startindex]); // counts back from startindex
What does the array sort function’s compare function return?
-1, 0, or 1 depending on whether a<b>b</b>
Turn [“Tom”, “Bill”, “Jerry”] into the string “Tom, Bill, Jerry”?
Use array.join(“, “);
Name two ways to find the 4th character in a string, “cats”
“cats”.charAt(3);
“cats”[3];
shift vs unshift?
myArray.shift() will remove the first item from an array and return it. Unshift(“val”) adds a value to start of array and returns new length.