0
0
Vueframework~20 mins

Computed properties for derived state in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Computed State Master
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 Vue computed property?
Consider this Vue component using the Composition API. What will be displayed inside the paragraph when the component renders?
Vue
<script setup>
import { ref, computed } from 'vue'
const count = ref(3)
const doubleCount = computed(() => count.value * 2)
</script>
<template>
  <p>{{ doubleCount }}</p>
</template>
Aundefined
B3
C6
DNaN
Attempts:
2 left
💡 Hint
Remember that computed properties return a reactive value based on other reactive sources.
state_output
intermediate
2:00remaining
What is the value of fullName computed property?
Given this Vue component, what is the value of the computed property fullName?
Vue
<script setup>
import { reactive, computed } from 'vue'
const user = reactive({ firstName: 'Jane', lastName: 'Doe' })
const fullName = computed(() => `${user.firstName} ${user.lastName}`)
</script>
Aundefined
B"Jane Doe"
C"Doe Jane"
D"JaneDoe"
Attempts:
2 left
💡 Hint
Look at the template literal combining firstName and lastName with a space.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a computed property that returns the square of a number?
Select the option that correctly defines a computed property named squared that returns the square of a reactive number value.
Aconst squared = computed(() => number.value ^ 2)
Bconst squared = computed(() => number * 2)
Cconst squared = computed(() => number.value * 2)
Dconst squared = computed(() => number.value ** 2)
Attempts:
2 left
💡 Hint
Remember to access the reactive value with .value and use the exponentiation operator.
🔧 Debug
advanced
2:00remaining
Why does this computed property not update when the reactive source changes?
Given this Vue component, why does the computed property fullName not update when user.firstName changes?
Vue
<script setup>
import { reactive, computed } from 'vue'
const user = { firstName: 'John', lastName: 'Smith' }
const fullName = computed(() => `${user.firstName} ${user.lastName}`)
user.firstName = 'Mike'
</script>
ABecause user is not reactive, so changes to firstName are not tracked.
BBecause computed properties cannot depend on objects.
CBecause fullName needs to be a ref, not computed.
DBecause user.firstName is a constant and cannot be changed.
Attempts:
2 left
💡 Hint
Check how user is declared and whether Vue can track its changes.
🧠 Conceptual
expert
2:00remaining
How does Vue's computed property caching work?
Which statement best describes how Vue caches computed properties?
AVue caches computed properties and only recalculates them when their reactive dependencies change.
BVue recalculates computed properties on every component render regardless of dependencies.
CVue caches computed properties permanently after the first calculation and never updates them.
DVue does not cache computed properties; they behave like methods.
Attempts:
2 left
💡 Hint
Think about efficiency and when Vue should update computed values.