Complete the code to define a computed property using Vue's Composition API.
<script setup lang="ts"> import { ref, computed } from 'vue' const count = ref(0) const doubleCount = computed(() => count.value[1]2) </script>
The computed property doubleCount multiplies count by 2 using the * operator.
Complete the code to type a computed property that returns a string.
<script setup lang="ts"> import { ref, computed } from 'vue' const firstName = ref('John') const lastName = ref('Doe') const fullName = computed<string>(() => firstName.value + ' ' + [1].value) </script>
firstName twice will duplicate the first name.fullName inside its own definition causes errors.The computed property fullName concatenates firstName and lastName. We use lastName here to complete the full name.
Fix the error in typing the computed property that returns a number.
<script setup lang="ts"> import { ref, computed } from 'vue' const items = ref<number[]>([1, 2, 3]) const total = computed<number>(() => items.value.reduce((acc, cur) => acc + cur, [1])) </script>
The reduce function needs a starting number 0 to sum the array correctly.
Fill both blanks to type a computed property that filters an array of strings by length.
<script setup lang="ts"> import { ref, computed } from 'vue' const words = ref<string[]>(['apple', 'banana', 'cherry', 'date']) const longWords = computed<string[]>(() => words.value.filter(word => word.length [1] [2])) </script>
The computed property filters words with length greater than 5.
Fill all three blanks to type a computed property that creates an object mapping words to their uppercase versions for words longer than 4 characters.
<script setup lang="ts"> import { ref, computed } from 'vue' const words = ref<string[]>(['vue', 'react', 'angular', 'svelte']) const upperMap = computed<Record<string, string>>(() => { return Object.fromEntries(words.value.filter(word => word.length [1] [2]).map(word => [[3], word.toUpperCase()])) }) </script>
The computed property filters words longer than 4 characters and maps each word to its uppercase version using the word itself as the key.