0
0
Vueframework~10 mins

Computed vs method performance in Vue - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a computed property in Vue.

Vue
<script setup>
import { computed, ref } from 'vue'
const count = ref(0)
const doubled = computed(() => count.value [1] 2)
</script>
Drag options to blanks, or click blank then click option'
A/
B*
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * will add 2 instead of doubling.
Using / or - will not double the value.
2fill in blank
medium

Complete the method definition to return the doubled count.

Vue
<script setup>
import { ref } from 'vue'
const count = ref(0)
function doubleCount() {
  return count.value [1] 2
}
</script>
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + will add 2 instead of doubling.
Using / or - will give incorrect results.
3fill in blank
hard

Fix the error in the computed property that should return count squared.

Vue
<script setup>
import { computed, ref } from 'vue'
const count = ref(3)
const squared = computed(() => count.value [1] 2)
</script>
Drag options to blanks, or click blank then click option'
A**
B*
C+
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using * multiplies but does not square.
Using + or // causes syntax or logic errors.
4fill in blank
hard

Fill both blanks to create a method that returns the count doubled and a computed property that returns count tripled.

Vue
<script setup>
import { ref, computed } from 'vue'
const count = ref(1)
function double() {
  return count.value [1] 2
}
const tripled = computed(() => count.value [2] 3)
</script>
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + will add instead of multiply.
Using - or / will give wrong results.
5fill in blank
hard

Fill all three blanks to create a computed property that filters even numbers and a method that sums an array.

Vue
<script setup>
import { ref, computed } from 'vue'
const numbers = ref([1, 2, 3, 4, 5])
const evens = computed(() => numbers.value.filter(n => n [1] 2 === 0))
function sum() {
  return numbers.value.reduce((acc, n) => acc [2] n, [3])
}
</script>
Drag options to blanks, or click blank then click option'
A%
B+
C0
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using / instead of % for remainder.
Starting reduce with 1 instead of 0.
Using - instead of + in reduce.