Skip to content
Exercise · Objects · lesson 13 of 13

Exercise: who and where from

Write a function function describeUser(user) that returns the string "<name> from <city>". The name is in the property user.name, the city in the property user.address.city. When there is no name, guest goes in its place, and when there is no address or no city in it, unknown.

  • describeUser({ name: "Ann", address: { city: "Oslo" } }) → "Ann from Oslo"
  • describeUser({ name: "Bob" }) → "Bob from unknown"
  • describeUser({ address: { city: "Rome" } }) → "guest from Rome"

Constraints: user is an object, possibly empty and with extra properties; name and city, when present, are non-empty strings; the property address may be missing, may be null, or may be an object without city.

Do not add extra imports unless the task says so explicitly.

describe-user.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way