Skip to content
intermediate

Review: a cart total that never moves

This is recall. The code looks finished and is wrong. Find the defect and fix it. You are graded on tests you cannot see.

You asked a model for a small cart composable: items, an `add` function, and a running total. It produced this, with tests. The tests pass.

import { reactive, ref, type Ref } from 'vue'

export interface CartItem {
  name: string
  price: number
  quantity: number
}

export function useCart() {
  const items = reactive<CartItem[]>([])

  const total: Ref<number> = ref(
    items.reduce((sum, item) => sum + item.price * item.quantity, 0)
  )

  function add(item: CartItem): void {
    items.push(item)
  }

  return { items, total, add }
}
review — find the defectCtrl+↵ submits · Ctrl+Shift+↵ runsstarter code