Bird
Raised Fist0
Vueframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the v-else directive do in Vue?
easy
A. Runs only if the first v-if condition is true
B. Runs when all previous v-if and v-else-if conditions are false
C. Runs independently of any other condition
D. Runs only if the last v-else-if condition is true

Solution

  1. Step 1: Understand v-if and v-else-if behavior

    v-if runs if its condition is true; v-else-if runs if the previous v-if or v-else-if was false and its own condition is true.
  2. Step 2: Understand v-else behavior

    v-else runs only if all previous v-if and v-else-if conditions are false, acting as a fallback.
  3. Final Answer:

    Runs when all previous v-if and v-else-if conditions are false -> Option B
  4. Quick Check:

    v-else = fallback [OK]
Hint: Remember: v-else is the fallback after all conditions fail [OK]
Common Mistakes:
  • Thinking v-else runs independently
  • Confusing v-else with v-else-if
  • Assuming v-else requires a condition
2. Which of the following is the correct syntax for using v-else-if in Vue?
easy
A. <div v-else-if="condition">Content</div>
B. <div v-else-if>condition</div>
C. <div v-else-if:condition>Content</div>
D. <div v-else-if(condition)>Content</div>

Solution

  1. Step 1: Recall Vue directive syntax

    Vue directives use the format v-directive="expression" to bind conditions or values.
  2. Step 2: Check each option's syntax

    <div v-else-if="condition">Content</div> uses v-else-if="condition", which is correct. Options B, C, and D use invalid syntax forms.
  3. Final Answer:

    <div v-else-if="condition">Content</div> -> Option A
  4. Quick Check:

    Directive syntax = v-else-if="condition" [OK]
Hint: Use quotes for conditions in v-else-if [OK]
Common Mistakes:
  • Omitting quotes around condition
  • Using parentheses or colons incorrectly
  • Writing v-else-if without a condition
3. Given this Vue template snippet:
<div v-if="score >= 90">A</div>
<div v-else-if="score >= 75">B</div>
<div v-else-if="score >= 60">C</div>
<div v-else>F</div>

What will be displayed if score is 80?
medium
A. A
B. C
C. B
D. F

Solution

  1. Step 1: Evaluate the first condition score >= 90

    Since 80 is less than 90, this condition is false, so this block does not render.
  2. Step 2: Evaluate the second condition score >= 75

    80 is greater than or equal to 75, so this condition is true. Vue renders this block and skips the rest.
  3. Final Answer:

    B -> Option C
  4. Quick Check:

    80 >= 75 = true, so output is B [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
  • Choosing the first condition without checking value
  • Ignoring order of v-else-if blocks
  • Assuming multiple blocks render
4. What is wrong with this Vue template?
<div v-if="isActive">Active</div>
<div v-else-if>Inactive</div>
<div v-else>Unknown</div>
medium
A. The v-else-if directive is missing a condition
B. The v-else directive cannot be used after v-else-if
C. The v-if directive must have a closing tag
D. There should be no v-else after v-if

Solution

  1. Step 1: Check v-else-if usage

    v-else-if requires a condition to evaluate. Here, it is used without any condition, which is invalid syntax.
  2. Step 2: Verify other directives

    v-else can follow v-else-if, and v-if has proper closing tags, so no issues there.
  3. Final Answer:

    The v-else-if directive is missing a condition -> Option A
  4. Quick Check:

    v-else-if needs a condition [OK]
Hint: v-else-if always needs a condition in quotes [OK]
Common Mistakes:
  • Leaving v-else-if without condition
  • Thinking v-else can't follow v-else-if
  • Confusing v-if closing tags
5. You want to display a message based on a user's status using Vue directives. The statuses are: 'admin', 'editor', and others. Which template correctly uses v-if, v-else-if, and v-else to show 'Admin Access', 'Editor Access', or 'Guest Access' respectively?
hard
A.
<div v-if="status === 'admin'">Admin Access</div>
<div v-else>Editor Access</div>
<div v-else-if="status === 'editor'">Guest Access</div>
B.
<div v-if="status === 'admin'">Admin Access</div>
<div v-if="status === 'editor'">Editor Access</div>
<div v-else>Guest Access</div>
C.
<div v-if="status === 'admin'">Admin Access</div>
<div v-else-if>Editor Access</div>
<div v-else>Guest Access</div>
D.
<div v-if="status === 'admin'">Admin Access</div>
<div v-else-if="status === 'editor'">Editor Access</div>
<div v-else>Guest Access</div>

Solution

  1. Step 1: Check the chaining of conditions

    <div v-if="status === 'admin'">Admin Access</div>
    <div v-else-if="status === 'editor'">Editor Access</div>
    <div v-else>Guest Access</div>
    correctly uses v-if, then v-else-if with a condition, then v-else as fallback.
  2. Step 2: Verify other options for errors

    <div v-if="status === 'admin'">Admin Access</div>
    <div v-if="status === 'editor'">Editor Access</div>
    <div v-else>Guest Access</div>
    uses two v-if directives, which are independent and can cause multiple blocks to render.
    <div v-if="status === 'admin'">Admin Access</div>
    <div v-else-if>Editor Access</div>
    <div v-else>Guest Access</div>
    uses v-else-if without a condition, which is invalid.
    <div v-if="status === 'admin'">Admin Access</div>
    <div v-else>Editor Access</div>
    <div v-else-if="status === 'editor'">Guest Access</div>
    places v-else before v-else-if, which breaks the chain.
  3. Final Answer:

    The template that correctly chains v-if, v-else-if with condition, and v-else -> Option D
  4. Quick Check:

    v-if, v-else-if with condition, then v-else [OK]
Hint: Chain conditions: v-if, v-else-if with condition, then v-else [OK]
Common Mistakes:
  • Using multiple v-if instead of v-else-if
  • Omitting condition in v-else-if
  • Placing v-else before v-else-if
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard