0
0
Vueframework~20 mins

Testing user interactions in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Interaction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the button is clicked?

Consider this Vue 3 component using the Composition API. What will be the displayed count after clicking the button twice?

Vue
<template>
  <button @click="increment">Clicked {{ count }} times</button>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count.value++
}
</script>
AClicked 0 times
BClicked 1 times
CClicked 2 times
DClicked NaN times
Attempts:
2 left
💡 Hint

Remember how ref works and how the increment function changes the value.

📝 Syntax
intermediate
2:00remaining
Which option correctly binds a click event in Vue 3?

Identify the correct syntax to bind a click event to a method named handleClick in a Vue 3 template.

Vue
<template>
  <!-- Choose the correct button syntax -->
</template>
A<button @click="handleClick">Click me</button>
B<button @click="handleClick()">Click me</button>
C<button v-on:click="handleClick">Click me</button>
D<button v-click="handleClick">Click me</button>
Attempts:
2 left
💡 Hint

Vue 3 supports shorthand for event binding. Consider if parentheses are needed.

🔧 Debug
advanced
2:00remaining
Why does this Vue 3 component not update the displayed message on button click?

Examine the code below. The message does not update when the button is clicked. What is the cause?

Vue
<template>
  <div>
    <p>{{ message }}</p>
    <button @click="updateMessage">Update</button>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
const state = reactive({ message: 'Hello' })
function updateMessage() {
  state.message = 'Updated!'
}
</script>
AThe template uses {{ message }} but the reactive object is named state, so message is undefined.
BReactive objects cannot be updated directly; use ref instead.
CThe updateMessage function is not called because the button has no click event.
DThe reactive object must be wrapped in ref to update properly.
Attempts:
2 left
💡 Hint

Check the variable names used in the template and script.

state_output
advanced
2:00remaining
What is the final value of count after these updates?

Given this Vue 3 component, what is the value of count after the updateCount function runs?

Vue
<script setup>
import { ref } from 'vue'
const count = ref(0)
function updateCount() {
  count.value += 1
  count.value += 2
  count.value = count.value * 2
}
updateCount()
</script>
A3
B6
C4
D5
Attempts:
2 left
💡 Hint

Calculate step-by-step how count.value changes.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue 3's reactive and ref is true?

Choose the correct statement about the difference between reactive and ref in Vue 3.

A<code>reactive</code> returns a ref object, while <code>ref</code> returns a reactive proxy.
B<code>reactive</code> can only be used with primitive values, <code>ref</code> only with objects.
C<code>ref</code> and <code>reactive</code> are interchangeable and have no differences.
D<code>ref</code> creates a reactive object for primitive values, while <code>reactive</code> creates a reactive proxy for objects and arrays.
Attempts:
2 left
💡 Hint

Think about how Vue tracks changes for simple values vs complex objects.