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?
The answer is: === on arrays compares identity, and those are two separate arrays
The answer is: === on arrays compares identity, and those are two separate arrays
The answer is: === on arrays compares identity, and those are two separate arrays
3.[3p] What exactly does const guarantee for const scores = [1, 2];?
What exactly does const guarantee for const scores = [1, 2];?
The answer is: That the name will always reach the same array, saying nothing about its contents
The answer is: That the name will always reach the same array, saying nothing about its contents
The answer is: That the name will always reach the same array, saying nothing about its contents
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
The answer is: [...values], { ...obj }, Object.assign({}, obj)
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.
The answer is: False
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
The answer is: A Date comes back as a string, Keys whose value is undefined disappear, It throws on a structure that contains a cycle
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?
The answer is: sort mutates, so the caller's array is silently reordered by a function that reads like a question
The answer is: sort mutates, so the caller's array is silently reordered by a function that reads like a question
The answer is: sort mutates, so the caller's array is silently reordered by a function that reads like a question