Strings and arrays Flashcards

1
Q

Turn ‘apples, bananas, oranges, pears and salsa’ into an array but without ‘and salsa’

A

theString.split( ‘ ‘, 4);

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

What does someString.split(“”) do (first arg is empty string)?

A

Converts string to an array of characters. “abc” becomes [‘a’,’b’,’c’];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
var arr = ["apple", "orange", "grape"]; 
var c = arr.push("banana"); 
what is c?
A

It returns the number 4 because push returns the length of the array, not the array.

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

var a = [a,b,c,d,e]. What does b = a.splice() do?

A

a will become an empty array and b will become [a,b,c,d,e]

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

someArray.splice(arg1, arg2); What is arg1 and arg2?

A

arg1 is the index to remove from; arg 2 is how many items after that.

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

How would you change [1,0,0,0,5] to [1, 2, 3, 4,5] using an array method?

A

theArray.splice( 1, 3, 2, 3 ,4);

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

What’s the best way to copy array myArr into newArr?

A

newArr = myArr.slice( ) or slice( 0 );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
var a = "My String"; 
var b = a.slice(1); 
What happens to a and to b?
A
a = 'My String'
b = 'y String'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are substr(), substring(), and slice() similar/different?

A

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;

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

arr1 = [ 0, 2, 4];
arr2 = [ 1, 3, 5];
How could I change arr1 to be 0,2,4,1,3,5 ?

A

arr1 = arr1.concat(arr2); // concat is non-destructive.

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

what are the location methods?

A

indexof(arrayOrString, [startIndex]); // counts forward from startindex
lastIndexOf(arrayOrString, [startindex]); // counts back from startindex

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

What does the array sort function’s compare function return?

A

-1, 0, or 1 depending on whether a<b>b</b>

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

Turn [“Tom”, “Bill”, “Jerry”] into the string “Tom, Bill, Jerry”?

A

Use array.join(“, “);

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

Name two ways to find the 4th character in a string, “cats”

A

“cats”.charAt(3);

“cats”[3];

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

shift vs unshift?

A

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.

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

When handling strings, what’s a handy function for dealing with white space?

A

trim(). Use this when doing things like str.split();

17
Q

What are the five iterative methods for arrays?

A

every(); some(); filter(); forEach(); map();

18
Q

When passing a function to an iterative array, what are the parameters you can include?

A

item, index, and array — the ‘array’ argument is the array being acted on.

19
Q
var arr = [1,2,3,4,5,6,7,8];
how could you create an array that had only the even values in arr?
A

var evenVals = arr.filter(function(item, index, array){ return item%2==0; });

20
Q
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?
A

var doubledArr = arr.map(function(item, index, array){ return item * 2; });

21
Q
var arr = [1,2,3,4,5,6,7,8];
What if you wanted to know if every item in this array was positive?
A

var allPositive = arr.every(function(item, index, array){ return item > 0; });

22
Q

How is array.filter() different from array.map()?

A

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.

23
Q

What is a concise and popular way to convert an array-like object to an actual array?

A
newArr = Array.prototype.slice.call( someNodeList ) or
newArr = [].slice.call( jqueryObj );
24
Q

What arguments does the myArray.reduce() function take and what does it do?

A

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.

25
Q

What are two ways to find if an array contains a particular value?

A

someArray.indexOf(myValue) > -1; // yes!

someArray.includes(myValue) === true; // yes!