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.

References and mutation

1.[2p]

const a = [1, 2]; const b = a; b.push(3); What does a.length evaluate to?

CorrectNot quite: 3

2.[2p]

Why is [1, 2] === [1, 2] false?

Correct
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];?

Correct
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?

Select all that apply

Correct
Correct
Correct
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?

CorrectNot quite: 3

6.[2p]

function replaceList(list) { list = ["new"]; } changes the array the caller passed in.

The answer is: False
Correct

7.[3p]

Which of these are true of JSON.parse(JSON.stringify(value)) as a deep copy?

Select all that apply

Correct
Correct
Correct
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.

  • 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?

Correct
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