Preview

11 - Working with Arrays and more on functions

 1. Select the statement that best describes this JavaScript command / property
a[i]

  Add value b to the end of the array.

  The number of elements in the array.

  If i is an integer, this will access the element at that position.

  Remove the last element of the array, and return it.

 2. Select the statement that best describes this JavaScript command / property
a.length

  If i is an integer, this will access the element at that position.

  Add value b to the end of the array.

  Remove the last element of the array, and return it.

  The number of elements in the array.

 3. Select the statement that best describes this JavaScript command / property
a.pop()

  Remove the last element of the array, and return it.

  Add value b to the end of the array.

  The number of elements in the array.

  If i is an integer, this will access the element at that position.

 4. Select the statement that best describes this JavaScript command / property
a.push(b)

  Add value b to the end of the array.

  Remove the last element of the array, and return it.

  The number of elements in the array.

  If i is an integer, this will access the element at that position.

 5. In the following example, fruits[1] is the first element
var fruits = [ "apple", "orange", "mango" ];

  FALSE

  TRUE

 6. If you wished to replace the third element 'plum' with 'pear', what would you do?
let fruits = ["Apple", "Orange", "Plum"];

alert( fruits[0] ); // Apple
alert( fruits[1] ); // Orange
alert( fruits[2] ); // Plum

  replace 'plum' = 'pear'

  fruits[plum]=[pear]

  pear = fruits[2]

  fruits[2] = 'Pear';

 7. What does the second line of code do in this example?
let fruits = ["Apple", "Orange", "Pear"];
alert( fruits.pop()

  remove the first element (which is "Apple") and delete it

  remove "Orange" and alert it

  remove "Apple" and alert it

  remove "Pear" and alert it

 8. What does the following code do?
let arr = ["Apple", "Orange", "Pear"];

for (let i = 0; i < arr.length; i++) {
  alert( arr[i] );
}

  Uses a for loop to alert the system is there are more than 0 elements in the array

  Uses a for loop to check for the length of the array. In this case it is '3' and '3' will be the ouput

  Uses a for loop to cycle through the items in an array and output (alert) "Apple", "Orange", "Pear"

  Uses a for loop, but does so incorrectly. It should start at 0 and there is no need for the i++ in JavaScript

 9. What is the ouput from the following HTML and JavaScript code?
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Arrays</h2>

<p>The best way to loop through an array is using a standard for loop:</p>

<p id="demo"></p>

<script>
var fruits, text, fLen, i;
fruits = ["Banana", "Orange", "Apple", "Mango"];
fLen = fruits.length;

text = "<ul>";
for (i = 2; i < fLen; i++) {
  text += "<li>" + fruits[i] + "</li>";
}
text += "</ul>";

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

  Banana, Orange

  Apple, Mango

  Orange, Apple

  Banana, Mango

 10. In JavaScript it is worth remember that arrays and objects are different. Arrays use numbered indexes and _______________

  objects use arrays as indexes

  objects use named indexes

  objects use non-numeric (e.g. alphabetical) indexes

  objects don't use indexes at all