0
0
Vueframework~20 mins

Computed with getter and setter in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Computed Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when updating a computed property with a setter?

Consider this Vue 3 component using a computed property with a getter and setter. What will be the displayed value after clicking the button once?

Vue
<script setup>
import { ref, computed } from 'vue'
const firstName = ref('John')
const lastName = ref('Doe')
const fullName = computed({
  get() {
    return `${firstName.value} ${lastName.value}`
  },
  set(value) {
    const parts = value.split(' ')
    firstName.value = parts[0] || ''
    lastName.value = parts[1] || ''
  }
})
function updateName() {
  fullName.value = 'Jane Smith'
}
</script>
<template>
  <div>
    <p>Full Name: {{ fullName }}</p>
    <button @click="updateName">Change Name</button>
  </div>
</template>
AFull Name: John Doe
BFull Name: John Smith
CFull Name: Jane Doe
DFull Name: Jane Smith
Attempts:
2 left
💡 Hint

Think about how the setter splits the string and updates the reactive refs.

📝 Syntax
intermediate
2:00remaining
Which computed property syntax correctly defines a getter and setter in Vue 3?

Choose the correct way to define a computed property with both getter and setter functions.

Acomputed({ get() { return count.value }, set(value) { count.value = value } })
Bcomputed(() => count.value, (value) => { count.value = value })
Ccomputed({ getter() { return count.value }, setter(value) { count.value = value } })
Dcomputed({ get: () => count.value, set: (value) => { count.value = value } })
Attempts:
2 left
💡 Hint

Remember the computed function accepts an object with get and set keys for getter and setter.

state_output
advanced
2:00remaining
What is the value of fullName after setting it to an empty string?

Given this Vue 3 component, what will fullName.value be after calling fullName.value = ''?

Vue
<script setup>
import { ref, computed } from 'vue'
const firstName = ref('Alice')
const lastName = ref('Wonderland')
const fullName = computed({
  get() {
    return `${firstName.value} ${lastName.value}`.trim()
  },
  set(value) {
    const parts = value.split(' ')
    firstName.value = parts[0] || ''
    lastName.value = parts[1] || ''
  }
})
fullName.value = ''
</script>
A"" (empty string)
B" " (a single space)
C"Alice Wonderland"
D" " (space trimmed to empty string)
Attempts:
2 left
💡 Hint

Check how the setter updates firstName and lastName when given an empty string.

🔧 Debug
advanced
2:00remaining
Why does this computed setter not update the reactive state?

Look at this Vue 3 code snippet. The setter is called but the displayed name does not update. What is the problem?

Vue
<script setup>
import { ref, computed } from 'vue'
const firstName = ref('Tom')
const lastName = ref('Jerry')
const fullName = computed({
  get() {
    return `${firstName.value} ${lastName.value}`
  },
  set(value) {
    const parts = value.split(' ')
    firstName.value = parts[0] || ''
    lastName.value = parts[1] || ''
  }
})
function changeName() {
  fullName.value = 'Spike Bulldog'
}
</script>
<template>
  <div>
    <p>{{ fullName }}</p>
    <button @click="changeName">Change</button>
  </div>
</template>
AThe computed property is missing a return statement in the getter.
BThe setter reassigns the refs instead of updating their .value properties.
CThe setter does not split the string correctly.
DThe changeName function is not called on button click.
Attempts:
2 left
💡 Hint

Remember how to update reactive refs in Vue 3.

🧠 Conceptual
expert
3:00remaining
How does Vue 3 track dependencies in computed properties with getters and setters?

Which statement best explains how Vue 3 tracks reactive dependencies when using computed properties with both getter and setter?

AVue tracks dependencies only when the setter updates reactive refs explicitly.
BVue tracks dependencies during both getter and setter executions equally.
CVue tracks dependencies only during the getter execution and ignores the setter for reactivity.
DVue does not track dependencies in computed properties with setters.
Attempts:
2 left
💡 Hint

Think about when Vue collects reactive dependencies for reactivity updates.