Challenge - 5 Problems
Nested Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested object property access
What is the output of the following TypeScript code?
Typescript
type User = {
id: number;
profile: {
name: string;
address: {
city: string;
zip: string;
};
};
};
const user: User = {
id: 1,
profile: {
name: "Alice",
address: {
city: "Wonderland",
zip: "12345"
}
}
};
console.log(user.profile.address.city);Attempts:
2 left
💡 Hint
Look at how the nested objects are accessed using dot notation.
✗ Incorrect
The code accesses the nested property 'city' inside 'address' inside 'profile' of the 'user' object. The value is "Wonderland".
❓ Predict Output
intermediate2:00remaining
Output of nested object with optional chaining
What will this TypeScript code print?
Typescript
type Product = {
id: number;
details?: {
name: string;
manufacturer?: {
name: string;
};
};
};
const product: Product = {
id: 101
};
console.log(product.details?.manufacturer?.name ?? "Unknown");Attempts:
2 left
💡 Hint
Optional chaining returns undefined if any part is missing, then the nullish coalescing operator provides a default.
✗ Incorrect
Since 'details' and 'manufacturer' are missing, optional chaining returns undefined, so the ?? operator returns "Unknown".
🔧 Debug
advanced2:00remaining
Identify the error in nested object type assignment
What error does this TypeScript code produce?
Typescript
type Config = {
server: {
host: string;
port: number;
};
};
const config: Config = {
server: {
host: "localhost",
port: "8080"
}
};Attempts:
2 left
💡 Hint
Check the type of 'port' in the object vs the type definition.
✗ Incorrect
The 'port' property is defined as a number but assigned a string "8080", causing a type error.
📝 Syntax
advanced2:00remaining
Which option correctly defines a nested object type?
Which of the following TypeScript type definitions correctly defines a nested object type for a 'Book' with 'title' (string) and 'author' (object with 'name' and 'age')?
Attempts:
2 left
💡 Hint
Look for the correct syntax for nested object types in TypeScript.
✗ Incorrect
Option A uses the correct object type syntax with curly braces and property types.
🚀 Application
expert3:00remaining
Calculate total price from nested order object
Given the following nested order object, what is the value of 'totalPrice' after running the code?
Typescript
type Item = { name: string; price: number; quantity: number };
type Order = {
id: number;
items: Item[];
};
const order: Order = {
id: 555,
items: [
{ name: "Pen", price: 1.5, quantity: 10 },
{ name: "Notebook", price: 3, quantity: 5 },
{ name: "Eraser", price: 0.5, quantity: 20 }
]
};
const totalPrice = order.items.reduce((sum, item) => sum + item.price * item.quantity, 0);
console.log(totalPrice);Attempts:
2 left
💡 Hint
Multiply price by quantity for each item and add all together.
✗ Incorrect
Pen: 1.5*10=15, Notebook: 3*5=15, Eraser: 0.5*20=10; sum is 15+15+10=40