Challenge - 5 Problems
NgFor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will be rendered by this Angular template?
Given the component's
items array and the template below, what will be the rendered output inside the <ul> element?Angular
items = ['apple', 'banana', 'cherry']; Template: <ul> <li *ngFor="let item of items; let i = index">{{i}}: {{item}}</li> </ul>
Attempts:
2 left
💡 Hint
Remember that
index starts at 0 and is assigned to i.✗ Incorrect
The *ngFor directive loops over items. The index variable starts at 0, so each <li> shows the index and the item. Option A matches this exactly.
📝 Syntax
intermediate2:00remaining
Which option correctly uses
*ngFor with a trackBy function?You want to optimize list rendering by using a
trackBy function named trackById. Which template syntax is correct?Attempts:
2 left
💡 Hint
The
trackBy syntax uses a colon, not an equals sign or parentheses.✗ Incorrect
The correct syntax for trackBy in *ngFor is trackBy: functionName without parentheses. Option C uses this correctly.
🔧 Debug
advanced2:00remaining
Why does this
*ngFor code cause a runtime error?Consider this template snippet:
- User: {{user.name}}, Odd: {{odd}}
users = [{name: 'Anna'}, {name: 'Bob'}];
What is the cause of the runtime error?Attempts:
2 left
💡 Hint
Check the list of valid local variables
*ngFor supports.✗ Incorrect
*ngFor supports index, first, last, even, and odd as local variables. However, you cannot assign let odd = odd because odd is a reserved keyword in this context. You must use a different variable name like let isOdd = odd.
❓ state_output
advanced2:00remaining
What is the value of
selectedItem after this template interaction?Given this component code and template:
Component:
selectedItem = null;
items = ['red', 'green', 'blue'];
Template:
- {{color}}
Selected: {{selectedItem}}
If the user clicks the second<li> (green), what is displayed in the paragraph?Attempts:
2 left
💡 Hint
Clicking an item sets
selectedItem to that item's value.✗ Incorrect
Clicking the second <li> sets selectedItem to 'green'. The paragraph then shows 'Selected: green'.
🧠 Conceptual
expert3:00remaining
Which statement about
*ngFor and change detection is true?Consider Angular's change detection and the
*ngFor directive. Which statement correctly explains how trackBy affects performance?Attempts:
2 left
💡 Hint
Think about how Angular identifies list items to optimize rendering.
✗ Incorrect
trackBy helps Angular identify items uniquely, so it can reuse existing DOM elements instead of recreating them, improving performance.