Preview

11 - Working with Arrays and more on functions

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

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

  The number of elements in the array.

  Add value b to the end of the array.

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

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

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

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

  Add value b to the end of the array.

  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.

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

  The number of elements in the array.

  Add value b to the end of the array.

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

  The number of elements in the array.

  Add value b to the end of the array.

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

  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" ];

  TRUE

  FALSE

 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

  fruits[2] = 'Pear';

  fruits[plum]=[pear]

  replace 'plum' = 'pear'

  pear = fruits[2]

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

  remove "Pear" and alert it

  remove "Orange" and alert it

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

  remove "Apple" 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, but does so incorrectly. It should start at 0 and there is no need for the i++ in JavaScript

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

  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 alert the system is there are more than 0 elements in the array

 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>

  Orange, Apple

  Apple, Mango

  Banana, Orange

  Banana, Mango

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

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

  objects don't use indexes at all

  objects use named indexes

  objects use arrays as indexes