0
0
Vueframework~20 mins

v-else and v-else-if in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Conditional Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Vue template render?

Given the following Vue template snippet, what will be rendered if status is 'loading'?

<template>
  <div>
    <p v-if="status === 'success'">Success!</p>
    <p v-else-if="status === 'loading'">Loading...</p>
    <p v-else>Error occurred</p>
  </div>
</template>
ASuccess!
BNothing is rendered
CError occurred
DLoading...
Attempts:
2 left
💡 Hint

Check which condition matches the status value.

📝 Syntax
intermediate
2:00remaining
Which option is a valid use of v-else in Vue?

Choose the correct Vue template snippet that uses v-else properly.

A
&amp;lt;p v-if="show"&amp;gt;Shown&amp;lt;/p&amp;gt;
&amp;lt;p v-else&amp;gt;Hidden&amp;lt;/p&amp;gt;
B
&amp;lt;p v-else&amp;gt;Hidden&amp;lt;/p&amp;gt;
&amp;lt;p v-if="show"&amp;gt;Shown&amp;lt;/p&amp;gt;
C
&amp;lt;p v-if="show"&amp;gt;Shown&amp;lt;/p&amp;gt;
&amp;lt;p v-else v-if="!show"&amp;gt;Hidden&amp;lt;/p&amp;gt;
D
&amp;lt;p v-if="show"&amp;gt;Shown&amp;lt;/p&amp;gt;
&amp;lt;p v-else="true"&amp;gt;Hidden&amp;lt;/p&amp;gt;
Attempts:
2 left
💡 Hint

v-else must immediately follow a v-if or v-else-if element.

🔧 Debug
advanced
2:00remaining
Why does this Vue template cause a compilation error?

Examine the Vue template below and identify the cause of the error.

<template>
  <div>
    <p v-if="isActive">Active</p>
    <span>Not active</span>
    <p v-else>Inactive</p>
  </div>
</template>
AThe <code>v-else</code> must be immediately after the <code>v-if</code> element without any other elements in between.
BThe <code>v-else</code> directive cannot be used on a <code>p</code> element.
CThe <code>v-if</code> directive is missing a closing tag.
DThe <code>v-else</code> must have a condition like <code>v-else-if</code>.
Attempts:
2 left
💡 Hint

Check the placement of v-else relative to v-if.

state_output
advanced
2:00remaining
What is the rendered output when count is 0?

Consider this Vue template and data:

<template>
  <div>
    <p v-if="count > 0">Positive</p>
    <p v-else-if="count === 0">Zero</p>
    <p v-else>Negative</p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
APositive
BZero
CNegative
DNo output
Attempts:
2 left
💡 Hint

Check the conditions in order and see which matches count.

🧠 Conceptual
expert
3:00remaining
Why must v-else not have a condition?

In Vue, v-else cannot have a condition like v-if or v-else-if. Why is this restriction important?

ABecause <code>v-else</code> elements are always hidden and never rendered.
BBecause Vue does not support multiple conditions on the same element.
CBecause <code>v-else</code> is a fallback that automatically applies when previous conditions fail, adding a condition would break this logic.
DBecause <code>v-else</code> must be used only inside <code>v-for</code> loops.
Attempts:
2 left
💡 Hint

Think about the purpose of v-else as a fallback.