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.
When handling strings, what’s a handy function for dealing with white space?
trim(). Use this when doing things like str.split();
What are the five iterative methods for arrays?
every(); some(); filter(); forEach(); map();
When passing a function to an iterative array, what are the parameters you can include?
item, index, and array — the ‘array’ argument is the array being acted on.
var arr = [1,2,3,4,5,6,7,8]; how could you create an array that had only the even values in arr?
var evenVals = arr.filter(function(item, index, array){ return item%2==0; });
var arr = [1,2,3,4,5,6,7,8]; how could you create an array which was the same as arr except every value was doubled?
var doubledArr = arr.map(function(item, index, array){ return item * 2; });
var arr = [1,2,3,4,5,6,7,8]; What if you wanted to know if every item in this array was positive?
var allPositive = arr.every(function(item, index, array){ return item > 0; });
How is array.filter() different from array.map()?
For each item, it evaluates an expression. It only returns that item if the expression is true. It doesn’t alter the item. Array.map() can return anything whether true or false.
What is a concise and popular way to convert an array-like object to an actual array?
newArr = Array.prototype.slice.call( someNodeList ) or newArr = [].slice.call( jqueryObj );
What arguments does the myArray.reduce() function take and what does it do?
The reduce function takes two arguments, a function and a startingValue. The function takes (accumulator, currentValue, index, array) and returns a value. The startingValue can be anything.
What are two ways to find if an array contains a particular value?
someArray.indexOf(myValue) > -1; // yes!
someArray.includes(myValue) === true; // yes!