A. Missing return statement inside computed function
B. count should not be a ref
C. computed should be imported from 'vuex'
D. doubleCount should be a method, not computed
Solution
Step 1: Check computed function body
The arrow function uses braces but does not return a value explicitly, so it returns undefined.
Step 2: Correct usage
Either remove braces or add a return statement to return count.value + 2.
Final Answer:
Missing return statement inside computed function -> Option A
Quick Check:
Computed with braces needs return [OK]
Hint: Add return when using braces in arrow function [OK]
Common Mistakes:
Forgetting return in block arrow functions
Importing computed from wrong package
Misusing ref as normal variable
5. You have a list of products with prices and quantities as refs. How would you create a computed property totalPrice that sums the product of price and quantity for all products?
Choose the correct computed property code snippet:
hard
A. const totalPrice = computed(() => products.reduce((sum, p) => sum + p.price * p.quantity, 0))
B. const totalPrice = computed(() => products.value.reduce((sum, p) => sum + p.price.value * p.quantity.value, 0))
C. const totalPrice = computed(() => { products.value.forEach(p => sum += p.price.value * p.quantity.value); return sum })
Each product has price and quantity as refs, so access with .value. products is a ref array, so use products.value.
Step 2: Choose correct aggregation method
Use reduce to sum all price * quantity values. const totalPrice = computed(() => products.value.reduce((sum, p) => sum + p.price.value * p.quantity.value, 0)) correctly uses reduce with .value access and initial sum 0.
Step 3: Analyze incorrect options
const totalPrice = computed(() => products.reduce((sum, p) => sum + p.price * p.quantity, 0)) misses .value on refs. const totalPrice = computed(() => { products.value.forEach(p => sum += p.price.value * p.quantity.value); return sum }) uses forEach but sum is undefined. const totalPrice = computed(() => products.value.map(p => p.price.value * p.quantity.value)) returns array, not sum.
Final Answer:
const totalPrice = computed(() => products.value.reduce((sum, p) => sum + p.price.value * p.quantity.value, 0)) -> Option B
Quick Check:
Use reduce with .value refs to sum totals [OK]
Hint: Use reduce with .value to sum reactive refs [OK]
Common Mistakes:
Forgetting .value on refs inside products
Using map instead of reduce for sum
Not initializing sum in reduce
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.