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.

Objects and nested data

1.[2p]

const key = "port"; What does config.key give when config is { host: "localhost", port: 8080 }?

Correct
The answer is: `undefined`, because dot notation looks for a key literally spelled `key`
The answer is: `undefined`, because dot notation looks for a key literally spelled `key`
The answer is: `undefined`, because dot notation looks for a key literally spelled `key`

2.[3p]

Why does order.payment.method throw while order.customer.email merely gives undefined?

Correct
The answer is: `order.payment` is `undefined` and `undefined` has no properties, whereas `order.customer` is an object that simply lacks that key
The answer is: `order.payment` is `undefined` and `undefined` has no properties, whereas `order.customer` is an object that simply lacks that key
The answer is: `order.payment` is `undefined` and `undefined` has no properties, whereas `order.customer` is an object that simply lacks that key

3.[2p]

What operator makes order.payment.method give undefined instead of throwing? Write the two characters.

CorrectNot quite: ?.

4.[3p]

Which of these survive a JSON.stringify and JSON.parse round trip unchanged?

Select all that apply

Correct
Correct
Correct
The answer is: A number, An array of strings, A nested object
The answer is: A number, An array of strings, A nested object

5.[3p]

In counts[key] = (counts[key] ?? 0) + amount, why is ?? used rather than ||?

Correct
The answer is: `||` would also replace a legitimate running total of 0, since 0 is falsy
The answer is: `||` would also replace a legitimate running total of 0, since 0 is falsy
The answer is: `||` would also replace a legitimate running total of 0, since 0 is falsy

6.[2p]

A tree has a root of size 0 with children of size 3 and 0, and the second of those has children of size 5 and 7. What does a recursive totalSize return for the root?

CorrectNot quite: 15

7.[3p]

Match each part of a recursive function to what it does.

  • Base case

  • Recursive case

  • Combining step

  • Stack overflow

  • answers directly, with no further call

  • what happens when the descent never reaches the base case

  • calls itself on smaller pieces of the same shape

  • turns the results of those calls into this call's answer

Show the answer

Base case: answers directly, with no further call Recursive case: calls itself on smaller pieces of the same shape Combining step: turns the results of those calls into this call's answer Stack overflow: what happens when the descent never reaches the base case

8.[2p]

Object keys are always strings, so counts[1] and counts["1"] reach the same entry.

Correct
The answer is: True

9.[2p]

When is a Map the better choice than a plain object?

Correct
The answer is: When the keys are data rather than field names you wrote yourself
The answer is: When the keys are data rather than field names you wrote yourself
The answer is: When the keys are data rather than field names you wrote yourself