0
0
Vueframework~20 mins

Getters for computed store values in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Getters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Vue 3 getter return?
Given the Vue 3 store with a getter that computes the total price, what is the output of store.totalPrice if store.items = [{ price: 10, qty: 2 }, { price: 5, qty: 3 }]?
Vue
import { reactive, computed } from 'vue';

const store = reactive({
  items: [],
  get totalPrice() {
    return this.items.reduce((sum, item) => sum + item.price * item.qty, 0);
  }
});

store.items = [{ price: 10, qty: 2 }, { price: 5, qty: 3 }];

console.log(store.totalPrice);
A35
B25
CSyntaxError
Dundefined
Attempts:
2 left
💡 Hint
Think about how the getter calculates the sum of price times quantity for each item.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Vue 3 getter for a computed store value?
You want to add a getter completedCount to a Vue 3 reactive store that counts completed tasks. Which option is syntactically correct?
Vue
import { reactive } from 'vue';

const store = reactive({
  tasks: [
    { id: 1, done: true },
    { id: 2, done: false }
  ],
  // Define getter here
});
AcompletedCount: () => this.tasks.filter(t => t.done).length,
Bfunction completedCount() { return this.tasks.filter(t => t.done).length; }
CcompletedCount() { return this.tasks.filter(t => t.done).length; }
Dget completedCount() { return this.tasks.filter(t => t.done).length; }
Attempts:
2 left
💡 Hint
Vue reactive getters use the get keyword inside the object.
state_output
advanced
2:00remaining
What is the output after updating the store items?
Consider this Vue 3 reactive store with a getter. After pushing a new item, what is the value of store.totalPrice?
Vue
import { reactive } from 'vue';

const store = reactive({
  items: [{ price: 7, qty: 1 }],
  get totalPrice() {
    return this.items.reduce((sum, item) => sum + item.price * item.qty, 0);
  }
});

store.items.push({ price: 3, qty: 4 });

console.log(store.totalPrice);
A10
B19
C7
DError: Cannot read property 'reduce' of undefined
Attempts:
2 left
💡 Hint
Remember the getter recalculates based on the current items array.
🔧 Debug
advanced
2:00remaining
Why does this Vue 3 getter not update reactively?
This Vue 3 store has a getter, but the UI does not update when items change. What is the cause?
Vue
import { reactive, computed } from 'vue';

const store = reactive({
  items: [],
  totalPrice: computed(() => {
    return store.items.reduce((sum, item) => sum + item.price * item.qty, 0);
  })
});

store.items.push({ price: 5, qty: 2 });
AUsing computed inside reactive object is incorrect; computed should be outside.
BReactive objects cannot have getters.
CThe getter should use 'this.items' instead of 'store.items'.
DThe push method does not trigger reactivity.
Attempts:
2 left
💡 Hint
Check how the getter accesses the reactive property inside the object.
🧠 Conceptual
expert
2:00remaining
How do Vue 3 getters differ from computed properties in stores?
Which statement best describes the difference between Vue 3 reactive getters and computed properties used in stores?
AGetters are defined as object properties with 'get' keyword and access reactive state via 'this'; computed properties are standalone functions that cache results.
BGetters and computed properties are identical and interchangeable in Vue 3 stores.
CComputed properties cannot access reactive state, but getters can.
DGetters automatically update UI, but computed properties do not.
Attempts:
2 left
💡 Hint
Think about syntax and how each accesses reactive data.