Course outline
Full-Stack course / JavaScript fundamentals

Safe profile lookup

Lab

How to fetch and submit this exercise

Once: install the tool and connect your account.

curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon login

Each exercise: download it, edit the file named in the instructions, then submit.

gonomicon fetch fs-week-2/03-03-safe-profile-lookup
cd 03-03-safe-profile-lookup
gonomicon submit

The runner depends on the exercise: JavaScript/TypeScript/React labs run with Vitest, Python labs with pytest, and config-file labs (Docker, CI, Terraform, Nginx) are checked by parsing your file. gonomicon test runs the visible checks on your computer first; submitting also runs the hidden checks on our server.

Safe profile lookup

Write two small functions that read nested fields off a user profile object, safely falling back when a piece is missing - without ever losing a real value that happens to be 0.

Worked example

Here's a similar pair of functions for a food truck's schedule data:

const truckOne = {
  name: "Golden Hour Tacos",
  location: { neighborhood: "Riverside", street: "5th Ave" },
  metrics: { rating: 4.8, ordersToday: 0 },
};

const truckTwo = {
  name: "Blue Pepper",
  metrics: { rating: 0, ordersToday: 12 },
};

function neighborhoodOrFallback(truck) {
  return truck.location?.neighborhood ?? "Unknown neighborhood";
}

function ratingOrZero(truck) {
  return truck.metrics?.rating ?? 0;
}

console.log(neighborhoodOrFallback(truckOne));
console.log(neighborhoodOrFallback(truckTwo));
console.log(ratingOrZero(truckOne));
console.log(ratingOrZero(truckTwo));
Riverside
Unknown neighborhood
4.8
0

truckOne has a location, so the chain reaches all the way through to neighborhood. truckTwo has no location at all, so truck.location?.neighborhood stops safely at undefined, and ?? swaps in the fallback string. ratingOrZero matters because truckTwo's rating really is 0 - and ?? only replaces null or undefined, so that real 0 survives untouched instead of being treated as "missing".

Your task

Write function cityOrFallback(profile). profile may or may not have an address object, and that address may or may not have a city. Return profile.address?.city, falling back to the exact string "Unknown city" when the chain comes up empty.

Write function ratingOrZero(profile). profile may or may not have a stats object, and that stats may or may not have a rating. Return profile.stats?.rating, falling back to 0 when the chain comes up empty - but a real rating of 0 must come back as 0, not get replaced.

Rules

  • Use optional chaining (?.) to read address.city and stats.rating safely - never a plain . that could crash on a missing object.
  • Use nullish coalescing (??) for both fallbacks - not ||, which would also replace a real 0 or an empty string.
  • Neither function changes the profile object it was given.

What you'll practice

Combining ?. and ?? to read a nested, sometimes-missing field with a sensible fallback, while keeping a real 0 (or other falsy-but-valid value) intact.

Run it

gonomicon fetch fs-week-2/03-03-safe-profile-lookup
cd 03-03-safe-profile-lookup
gonomicon test
gonomicon submit

Hints

  1. profile.address?.city is the whole job of the first check - no if statement needed, optional chaining already gives you undefined safely when address is missing.
  2. ?? only replaces null or undefined - a real rating of 0 should sail straight through, not get swapped for the fallback.
  3. Chain both operators together on one line: profile.stats?.rating ?? 0.