Arrays
1.[3p] What does [10, 9, 1, 2].sort() produce? Write the four numbers in order, separated by commas, with no spaces.
What does [10, 9, 1, 2].sort() produce? Write the four numbers in order, separated by commas, with no spaces.
2.[2p] Why does sort() with no argument put 10 before 9?
Why does sort() with no argument put 10 before 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"
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?
What does reading an index past the end of an array give in JavaScript?
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?
Which of these array methods change the array they are called on?
Select all that apply
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?
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?
6.[2p] const xs = [1, 2]; makes xs.push(3) an error, because const forbids changing the value.
const xs = [1, 2]; makes xs.push(3) an error, because const forbids changing the value.
The answer is: False
7.[2p] Put these lines in the order they must appear in a function that builds a new array.
Put these lines in the order they must appear in a function that builds a new array.
return out;const out = [];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.
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?
Why does indexOf return -1 for a value that is absent, rather than 0?
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