Implement program flow (25%) Flashcards
What does the == operator do?
Evaluates whether the values are equal independent of the underlying data type.
var n = 2000, s = '2000'; alert(n == s); // True alert(n === s); // False, since the data types don't match
What does the === operator do?
Evaluates whether the values are equal both in value and underlying data type.
var n = 2000, s = '2000'; alert(n == s); // True alert(n === s); // False, since the data types don't match
What are the two ways to create arrays?
var cars = ["Saab", "Volvo", "BMW"]; var cars = new Array("Saab", "Volvo", "BMW");
Why should you avoid using the new keyword to create arrays?
Avoid using the new keyword to create arrays var numbers = new Array(40, 300); // creates array with 2 element var numbers = new Array(40); // creates array with 40 undefined elements
What kind of indexing do arrays support?
numeric
How do you create an array that supports string indexing?
use an object instead
How do you resize an array?
var anArray = new Array(5);
alert(anArray.length);
anArray[9] = ‘soccer’; // anArray is dynamically resized
alert(anArray.length);
What does the indexOf method of arrays do?
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
What does the join method of arrays do?
Joins all elements of an array into a string.
What does the lastIndexOf method of arrays do?
Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
What does the pop method of arrays do?
Removes the last element from an array and returns that element.
What does the push method of arrays do?
Adds one or more elements to the end of an array and returns the new length of the array.
What does the reverse method of arrays do?
Reverses the order of the elements of an array in place — the first becomes the last, and the last becomes the first.
What does the shift method of arrays do?
Removes the first element from an array and returns that element.
What does the sort method of arrays do?
Sorts the elements of an array in place and returns the array.
What does the splice method of arrays do?
Adds and/or removes elements from an array.
What does the toString method of arrays do?
Returns a string representing the array and its elements.
What does the unshift method of arrays do?
Adds one or more elements to the front of an array and returns the new length of the array.
What does the valueOf method of arrays do?
Returns the array as a comma separated string.
What does the concat method of arrays do?
Returns a new array comprised of this array joined with other array(s) and/or value(s).
What does the slice method of arrays do?
Extracts a section of an array and returns a new array.
Which methods of arrays return new arrays?
concat, slice, sort, splice
How do you create a multi-dimensional array?
var multiArray = new Array(3); multiArray[0] = new Array(3); multiArray[1] = new Array(3); multiArray[2] = new Array(3); multiArray[1][2] = 'ball sport';
What are the parameters for the slice method of the Array object?
startIndex, endIndex
var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); var someSports = sports.slice(1, 2);
The slice method takes two parameters: the indexes where the slice operation should begin and end. The ending index isn’t included in the slice. All copied elements are returned as an array from the slice method. In this example, because ‘basketball’ is at index 1 and the ending index is specified at index 2, the resulting array someSports contains only one element ‘basketball’.