Create a simple quiz application in Vue with multiple questions and scoring.

Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
<template>
  <div v-if="!isFinished">
    <h2>{{ currentQuestion.question }}</h2>
    <input v-model="userAnswer" />
    <button @click="submitAnswer">Submit</button>
  </div>
  <h2 v-else>Quiz Over! Score: {{ score }}</h2>
</template>

<script setup>
import { ref, computed } from 'vue';

const questions = ref([
  { question: 'What is 2 + 2?', answer: '4' },
  { question: 'Capital of France?', answer: 'Paris' }
]);
const currentIndex = ref(0);
const score = ref(0);
const userAnswer = ref('');

const currentQuestion = computed(() => questions.value[currentIndex.value]);
const isFinished = computed(() => currentIndex.value >= questions.value.length);

const submitAnswer = () => {
  if (userAnswer.value.toLowerCase() === currentQuestion.value.answer.toLowerCase()) {
    score.value++;
  }
  userAnswer.value = '';
  currentIndex.value++;
};
</script>