References and mutation
1.[2p] const a = [1, 2]; const b = a; b.push(3); What does a.length evaluate to?
const a = [1, 2]; const b = a; b.push(3); What does a.length evaluate to?
2.[2p] Why is [1, 2] === [1, 2] false?
Why is [1, 2] === [1, 2] false?
3.[3p] What exactly does const guarantee for const scores = [1, 2];?
What exactly does const guarantee for const scores = [1, 2];?
4.[3p] Which of these produce a shallow copy, sharing anything nested inside?
Which of these produce a shallow copy, sharing anything nested inside?
Select all that apply
5.[3p] const a = [1, [2, 3]]; const b = [...a]; b[1].push(4); How many elements does a[1] now hold?
const a = [1, [2, 3]]; const b = [...a]; b[1].push(4); How many elements does a[1] now hold?
6.[2p] function replaceList(list) { list = ["new"]; } changes the array the caller passed in.
function replaceList(list) { list = ["new"]; } changes the array the caller passed in.
7.[3p] Which of these are true of JSON.parse(JSON.stringify(value)) as a deep copy?
Which of these are true of JSON.parse(JSON.stringify(value)) as a deep copy?
Select all that apply
8.[3p] Match each function name to what a reader should expect it to do.
Match each function name to what a reader should expect it to do.
sorted(values)
sort(values)
withItem(basket, x)
addItem(basket, x)
changes the basket it was given
reorders the array it was given
returns a new sorted array
returns a new basket
Show the answer
sorted(values): returns a new sorted array sort(values): reorders the array it was given withItem(basket, x): returns a new basket addItem(basket, x): changes the basket it was given
9.[3p] function topThree(values) { return values.sort((a, b) => b - a).slice(0, 3); } What is wrong with it?
function topThree(values) { return values.sort((a, b) => b - a).slice(0, 3); } What is wrong with it?