Sign in

Libre University uses your GitHub account. Signing in is only needed to sit a final test, so the score is kept on your profile.

Arrays

1.[3p]

What does [10, 9, 1, 2].sort() produce? Write the four numbers in order, separated by commas, with no spaces.

CorrectNot quite: 1,10,2,9

2.[2p]

Why does sort() with no argument put 10 before 9?

Correct
The answer is: It converts each element to a string and sorts those alphabetically, and `"10"` precedes `"9"`
The answer is: It converts each element to a string and sorts those alphabetically, and `"10"` precedes `"9"`
The answer is: It converts each element to a string and sorts those alphabetically, and `"10"` precedes `"9"`

3.[2p]

What does reading an index past the end of an array give in JavaScript?

Correct
The answer is: `undefined`, with no error, so the failure appears somewhere later
The answer is: `undefined`, with no error, so the failure appears somewhere later
The answer is: `undefined`, with no error, so the failure appears somewhere later

4.[3p]

Which of these array methods change the array they are called on?

Select all that apply

Correct
Correct
Correct
The answer is: `push`, `sort`, `splice`
The answer is: `push`, `sort`, `splice`

5.[2p]

const xs = [4, 8, 15]; let total = 0; for (let i = 0; i <= xs.length; i++) total += xs[i]; What does total hold at the end?

CorrectNot quite: NaN

6.[2p]

const xs = [1, 2]; makes xs.push(3) an error, because const forbids changing the value.

The answer is: False
Correct

7.[2p]

Put these lines in the order they must appear in a function that builds a new array.

  1. return out;

  2. const out = [];

  3. for (const v of values) out.push(v * v);

Show the answer

c, b, a

8.[3p]

Match each walk over an array to what it produces.

  • Combining every element into one value

  • Keeping only the elements that pass a test

  • Replacing each element with something computed from it

  • Stopping at the first element that matches

  • a shorter array

  • an array of the same length

  • an index, or -1 if absent

  • a single number, such as a mean

Show the answer

Combining every element into one value: a single number, such as a mean Keeping only the elements that pass a test: a shorter array Replacing each element with something computed from it: an array of the same length Stopping at the first element that matches: an index, or -1 if absent

9.[2p]

Why does indexOf return -1 for a value that is absent, rather than 0?

Correct
The answer is: `-1` is not a valid index, so it cannot be mistaken for the position of a real element
The answer is: `-1` is not a valid index, so it cannot be mistaken for the position of a real element
The answer is: `-1` is not a valid index, so it cannot be mistaken for the position of a real element