0
0
Angularframework~20 mins

ngDoCheck for custom change detection in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ngDoCheck Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does ngDoCheck affect component updates?

Consider an Angular component that implements ngDoCheck to detect changes in an object property. What happens when the object reference does not change but a nested property is updated?

Angular
export class MyComponent implements DoCheck {
  data = { count: 0 };
  oldCount = 0;

  ngDoCheck() {
    if (this.data.count !== this.oldCount) {
      console.log('Count changed:', this.data.count);
      this.oldCount = this.data.count;
    }
  }
}
AThe console logs 'Count changed:' every time the nested property changes, even if the object reference is the same.
BThe console never logs anything because <code>ngDoCheck</code> only detects reference changes, not nested property changes.
CThe console logs 'Count changed:' only when the object reference changes, not when nested properties change.
DThe console logs 'Count changed:' only once when the component initializes.
Attempts:
2 left
💡 Hint

Think about how ngDoCheck lets you write your own logic to detect changes beyond Angular's default checks.

lifecycle
intermediate
1:30remaining
When is ngDoCheck called in Angular's lifecycle?

Which statement best describes when Angular calls the ngDoCheck lifecycle hook?

A<code>ngDoCheck</code> is called only when the component is destroyed.
B<code>ngDoCheck</code> is called only once after the component is initialized.
C<code>ngDoCheck</code> is called after every change detection cycle, even if no input properties changed.
D<code>ngDoCheck</code> is called only when input properties change.
Attempts:
2 left
💡 Hint

Think about how Angular runs change detection frequently to keep the UI updated.

🔧 Debug
advanced
2:00remaining
Why does this ngDoCheck implementation cause performance issues?

Review this ngDoCheck method. Why might it cause performance problems?

Angular
ngDoCheck() {
  for (let i = 0; i < 1000000; i++) {
    // heavy computation
    Math.sqrt(i);
  }
  console.log('Checked');
}
ABecause <code>ngDoCheck</code> runs only on input changes, the computation is unnecessary.
BBecause <code>ngDoCheck</code> only runs once, the heavy computation is wasted.
CBecause <code>ngDoCheck</code> is asynchronous, the computation blocks UI updates.
DBecause <code>ngDoCheck</code> runs very often, heavy computations inside it slow down the app.
Attempts:
2 left
💡 Hint

Remember how often Angular runs change detection and calls ngDoCheck.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this ngDoCheck implementation

Which option contains the correct syntax for implementing ngDoCheck in an Angular component?

A
ngDoCheck() {
  console.log('Checking...');
}
B
ngDoCheck {
  console.log('Checking...');
}
C
ngDoCheck() =&gt; {
  console.log('Checking...');
}
D
function ngDoCheck() {
  console.log('Checking...');
}
Attempts:
2 left
💡 Hint

Remember how methods are declared inside a class in TypeScript.

state_output
expert
2:30remaining
What is the console output after these changes with ngDoCheck?

Given this component code, what will be logged to the console after the sequence of changes?

Angular
export class CounterComponent implements DoCheck {
  counter = { value: 0 };
  oldValue = 0;

  ngDoCheck() {
    if (this.counter.value !== this.oldValue) {
      console.log('Counter changed to', this.counter.value);
      this.oldValue = this.counter.value;
    }
  }

  increment() {
    this.counter.value++;
  }

  replaceCounter() {
    this.counter = { value: 10 };
  }
}

// Sequence:
// 1. increment()
// 2. ngDoCheck runs
// 3. replaceCounter()
// 4. ngDoCheck runs
// 5. increment()
// 6. ngDoCheck runs
A
Counter changed to 1
Counter changed to 10
B
Counter changed to 1
Counter changed to 10
Counter changed to 11
C
Counter changed to 1
Counter changed to 11
D
Counter changed to 10
Counter changed to 11
Attempts:
2 left
💡 Hint

Track the value changes carefully and when ngDoCheck logs happen.