Command Palette

Search for a command to run...

Hectal
Phase 6: Objects
JavaScript · Exercise 2 of 5 PreviousNext
Beginner

Read properties safely

Write getCity(user): it returns user.address.city, or "Unknown" when the user, the address or the city is missing (null or undefined). An empty string "" is a real value: return it as it is.

Write hasKey(obj, key): it returns true only if obj has key as its own property (inherited ones like toString don't count), even if the value is undefined.

What Check tests

  1. getCity returns the city when it exists
  2. getCity returns "Unknown" for a missing address, user or city
  3. An empty city "" is kept
  4. hasKey finds own keys, even holding undefined
  5. hasKey ignores inherited keys like toString

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.