0
0
Vueframework~20 mins

Computed in Composition API in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Computed Mastery in Composition API
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this computed property?
Consider this Vue 3 Composition API code snippet. What will be the displayed value of fullName when firstName is 'Jane' and lastName is 'Doe'?
Vue
import { ref, computed } from 'vue';

const firstName = ref('Jane');
const lastName = ref('Doe');

const fullName = computed(() => `${firstName.value} ${lastName.value}`);

// Assume fullName.value is displayed in template
A"firstName lastName"
B"Doe Jane"
C"Jane Doe"
D"JaneDoe"
Attempts:
2 left
💡 Hint
Remember computed properties return a value based on reactive refs.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a computed property that doubles a number?
You want to create a computed property doubleCount that returns twice the value of a reactive count. Which code snippet is correct?
Vue
import { ref, computed } from 'vue';
const count = ref(5);
Aconst doubleCount = computed(() => count.value * 2);
Bconst doubleCount = computed(count * 2);
Cconst doubleCount = computed(() => count.value ** 2);
Dconst doubleCount = computed(() => count * 2);
Attempts:
2 left
💡 Hint
Remember to access the value of a ref with .value inside computed.
state_output
advanced
2:00remaining
What is the value of fullName.value after updating lastName?
Given this code, what will fullName.value be after lastName.value = 'Smith'?
Vue
import { ref, computed } from 'vue';
const firstName = ref('John');
const lastName = ref('Doe');
const fullName = computed(() => `${firstName.value} ${lastName.value}`);

lastName.value = 'Smith';
A"Smith John"
B"John Smith"
C"John Doe"
D"Doe Smith"
Attempts:
2 left
💡 Hint
Computed properties update automatically when dependencies change.
🔧 Debug
advanced
2:00remaining
Why does this computed property not update when count changes?
Identify the reason why doubleCount does not update when count.value changes in this code:
Vue
import { ref, computed } from 'vue';
const count = ref(1);
const doubleCount = computed(() => count * 2);

count.value = 3;
ABecause count is not accessed with .value inside computed, so reactivity is lost.
BBecause computed properties cannot depend on refs.
CBecause count.value was not initialized before computed.
DBecause computed properties require async functions.
Attempts:
2 left
💡 Hint
Check how reactive refs are accessed inside computed functions.
🧠 Conceptual
expert
2:00remaining
What happens if a computed property has a setter but you only use its getter?
In Vue 3 Composition API, you define a computed property with both a getter and a setter. If you only read the computed property without calling the setter, what is the effect?
Vue
import { ref, computed } from 'vue';
const count = ref(0);
const doubleCount = computed({
  get() { return count.value * 2; },
  set(value) { count.value = value / 2; }
});

// Only reading doubleCount.value
AThe computed property becomes read-only and cannot be accessed.
BThe setter runs automatically to keep count.value in sync.
CAn error occurs because the setter is defined but not used.
DThe getter runs and returns count.value * 2; the setter is not called and count.value stays the same.
Attempts:
2 left
💡 Hint
Think about when setters are triggered in computed properties.