Safe profile lookup
How to fetch and submit this exercise
Once: install the tool and connect your account.
curl -fsSL https://gonomicon.com/install.sh | sh
gonomicon loginEach 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 submitSafe 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 readaddress.cityandstats.ratingsafely - never a plain.that could crash on a missing object. - Use nullish coalescing (
??) for both fallbacks - not||, which would also replace a real0or an empty string. - Neither function changes the
profileobject 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
- 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.
- ?? only replaces null or undefined - a real rating of 0 should sail straight through, not get swapped for the fallback.
- Chain both operators together on one line: profile.stats?.rating ?? 0.