Challenge - 5 Problems
Vue Template Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Output of JavaScript expression in Vue template interpolation
What will be displayed inside the
<div> when this Vue component renders?Vue
<template>
<div>{{ message.split('').reverse().join('') }}</div>
</template>
<script setup>
const message = 'hello'
</script>Attempts:
2 left
💡 Hint
Think about what the split, reverse, and join methods do to a string.
✗ Incorrect
The expression splits the string into characters, reverses the array, then joins it back into a string, resulting in the reversed word 'olleh'.
📝 Syntax
intermediate2:00remaining
Valid JavaScript expression in Vue template
Which option is a valid JavaScript expression inside a Vue template interpolation to show the square of
num?Vue
<template>
<div>{{ /* expression here */ }}</div>
</template>
<script setup>
const num = 4
</script>Attempts:
2 left
💡 Hint
Remember the exponentiation operator in JavaScript.
✗ Incorrect
Option C correctly uses the exponentiation operator '**' to square the number. Option C misses the second argument for Math.pow. Option C is incomplete syntax. Option C calls an undefined function.
🔧 Debug
advanced2:00remaining
Why does this Vue template expression cause an error?
Consider this Vue template snippet:
<div>{{ user.name.toUpperCase() }}</div>
If user is null, what error will occur and why?Vue
<template>
<div>{{ user.name.toUpperCase() }}</div>
</template>
<script setup>
const user = null
</script>Attempts:
2 left
💡 Hint
Think about what happens when you try to access a property on null.
✗ Incorrect
Since user is null, trying to access user.name causes a TypeError because null has no properties.
❓ state_output
advanced2:00remaining
Output after reactive state change in Vue template expression
Given this Vue component, what will be the displayed output after the button is clicked once?
Vue
<template>
<div>{{ count.value * 3 }}</div>
<button @click="increment">Add</button>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(2)
function increment() {
count.value++
}
</script>Attempts:
2 left
💡 Hint
Remember that count is reactive and starts at 2, then increments by 1.
✗ Incorrect
Initially count is 2, after clicking increment once, count becomes 3. The template shows count * 3, so 3 * 3 = 9.
🧠 Conceptual
expert2:00remaining
Understanding expression evaluation order in Vue templates
In a Vue template, what is the result of this expression if
a = 5 and b = 0?
{{ a && b || a }}Vue
<template>
<div>{{ a && b || a }}</div>
</template>
<script setup>
const a = 5
const b = 0
</script>Attempts:
2 left
💡 Hint
Recall how JavaScript evaluates && and || operators from left to right.
✗ Incorrect
The expression evaluates as (a && b) || a. Since a=5 (truthy), a && b returns b which is 0 (falsy). Then 0 || a returns 5. So the final result is 5.