Bird
0
0

Which code correctly updates the full name when either signal changes and avoids unnecessary recomputations?

hard📝 Application Q15 of 15
Angular - Signals
You want to create a computed signal that returns the full name by combining two signals: firstName and lastName. Which code correctly updates the full name when either signal changes and avoids unnecessary recomputations?
Aconst fullName = computed(() => firstName() + lastName());
Bconst fullName = signal(`${firstName()} ${lastName()}`);
Cconst fullName = computed(() => firstName + ' ' + lastName);
Dconst fullName = computed(() => `${firstName()} ${lastName()}`);
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    The computed signal should combine firstName and lastName signals and update automatically when either changes.
  2. Step 2: Evaluate each option

    const fullName = computed(() => `${firstName()} ${lastName()}`); correctly calls both signals as functions and concatenates with a space. const fullName = signal(`${firstName()} ${lastName()}`); uses signal which won't update automatically. const fullName = computed(() => firstName + ' ' + lastName); uses signal variables directly without calling them. const fullName = computed(() => firstName() + lastName()); concatenates without space.
  3. Final Answer:

    const fullName = computed(() => `${firstName()} ${lastName()}`); -> Option D
  4. Quick Check:

    Call signals with () and combine with space [OK]
Quick Trick: Use computed with template literals calling signals () [OK]
Common Mistakes:
  • Using signal() instead of computed() for derived values
  • Not calling signals as functions
  • Forgetting space between names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes