Implement program flow (25%) Flashcards Preview

Exam 70-480: Programming in HTML5 with JavaScript and CSS3 > Implement program flow (25%) > Flashcards

Flashcards in Implement program flow (25%) Deck (134)
Loading flashcards...
1

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

2

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

3

What are the two ways to create arrays?

var cars = ["Saab", "Volvo", "BMW"];
var cars = new Array("Saab", "Volvo", "BMW");

4

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

5

What kind of indexing do arrays support?

numeric

6

How do you create an array that supports string indexing?

use an object instead

7

How do you resize an array?

var anArray = new Array(5);
alert(anArray.length);
anArray[9] = ‘soccer’; // anArray is dynamically resized
alert(anArray.length);

8

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.

9

What does the join method of arrays do?

Joins all elements of an array into a string.

10

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.

11

What does the pop method of arrays do?

Removes the last element from an array and returns that element.

12

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.

13

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.

14

What does the shift method of arrays do?

Removes the first element from an array and returns that element.

15

What does the sort method of arrays do?

Sorts the elements of an array in place and returns the array.

16

What does the splice method of arrays do?

Adds and/or removes elements from an array.

17

What does the toString method of arrays do?

Returns a string representing the array and its elements.

18

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.

19

What does the valueOf method of arrays do?

Returns the array as a comma separated string.

20

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).

21

What does the slice method of arrays do?

Extracts a section of an array and returns a new array.

22

Which methods of arrays return new arrays?

concat, slice, sort, splice

23

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';

24

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'.

25

What are the parameters for the splice method of the Array object?

startIndex, length, optional items to add to the array

var sports = new Array('soccer', 'basketball', 'hockey',
'football', 'cricket', 'rugby', 'tennis', 'badminton');
var splicedItems = sports.splice(1, 3, 'golf', 'curling',
'darts');

The splice method returns an array containing the items that are spliced out of the source that are spliced out of the source array. The first parameter is the index in the array where the splice operation should start. The second parameter is the number of items to splice, starting from the index specified in the first parameter. The optional last parameter lists items that are to replace the items being spliced out. The list doesn’t have to be the same length as the items being spliced out. In fact, if the last parameter is omitted, the spliced items are simply removed from the array and not replaced. In this example, three items are replaced, starting at index 1. So, 'basketball', 'hockey', and 'football' are replaced with 'golf', 'curling' and 'darts'.

26

Which methods of the Array object are used to provide stack functionality?

pop and push

var sports = new Array();
sports.push('soccer', 'basketball', 'hockey');
sports.push('football');
var nextSport = sports.pop();

27

Which methods of the Array object are used to provide queue functionality?

push and shift

Array.prototype.enqueue = function(item)
{
this.push(item);
};

Array.prototype.dequeue = function()
{
return this.shift();
};

var queue = new Array();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);

var next = queue.dequeue();

28

Which methods of the Array object operate only on the beginning of the array?

shift and unshift

var sports = new Array();
sports.unshift('soccer', 'basketball', 'hockey');
sports.unshift('football');
var nextSport = sports.shift();

The shift and unshift methods work in the exact opposite way from the pop and push methods. The shift method removes and returns the first element of the array, whereas the unshift method inserts new elements at the beginning of the array.

29

Which method of the Array object tests whether ALL elements in the array pass the test implemented by the provided function?

every

var evenNumbers = new Array(0, 2, 4, 6, 8, 9, 10, 12);
var allEven = evenNumbers.every(evenNumberCheck, this);

// allEven is false since 9 is not even
if (allEven) {
...
} else {
...
}

function evenNumberCheck(value, index, array) {
return (value % 2) == 0;
}

30

Which method of the Array object tests whether ANY elements in the array pass the test implemented by the provided function?

some

var evenNumbers = new Array(0, 2, 4, 6, 8, 9, 10, 12);
var someEven = evenNumbers.some(evenNumberCheck, this);

// someEven is true even though 9 is not even
if (someEven) {
...
} else {
...
}

function evenNumberCheck(value, index, array) {
return (value % 2) == 0;
}