Upon stumbling upon this Codepen showcasing a personality quiz designed for an HTML file with a Vue instance, I took it upon myself to transform it into a Vue.js file. However, despite making some minor modifications to align it with Vue.js standards, I encountered the following error message:
45:17 error 'quiz' is assigned a value but never used no-unused-vars
139:19 error 'quiz' is not defined no-undef
I recall reading similar queries regarding variables being assigned but left unused, so I added a console.log(quiz.title) line which seemingly resolved the initial error (although I'm unsure if this is the optimal solution). Yet, the second error relating to 'quiz' remaining undefined continues to puzzle me. What could be causing this issue?
<template>
<div id="app" v-cloak>
<div class="row">
<div class="large-12 columns">
<h1>{{ quiz.title }}</h1>
<div class="callout">
<div v-for="(question, index) in quiz.questions" v-bind:key="question.id">
<div v-show="index === questionIndex">
<h3>{{ question.text }}</h3>
<ol>
<li v-for="response in question.responses" v-bind:key="response.id">
<label>
<input type="radio" v-bind:value="response.value" v-bind:name="index" v-model="userResponses[index]">{{response.text}}
</label>
</li>
</ol>
<button class="secondary button" v-if="questionIndex > 0" v-on:click="prev">prev</button>
<button class="success button" v-on:click="next">next</button>
</div>
</div>
<div v-show="questionIndex === quiz.questions.length">
<h3>Your Results</h3>
<p>You are: {{ score() }}</p>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'PersonalityTest',
components: {
},
created() {
window.addEventListener("load", this.onWindowLoad);
},
methods: {
onWindowLoad() {
let quiz = {
title: 'What superhero are you?',
questions: [{
id: 1,
text: "How would you describe your personality?",
responses: [{
text: 'Im serious and dark',
value: 'Batman'
},
{
text: 'Arrogant, but charming',
value: 'Superman'
},
{
text: 'Fun and easy going',
value: 'The Flash'
}
]
},
{
id: 2,
text: "Why did you want to become a superhero?",
responses: [{
text: 'For the thrills',
value: 'The Flash'
},
{
text: 'For justice',
value: 'Batman'
},
{
text: 'For popularity',
value: 'Superman'
}
]
},
{
id: 3,
text: "Who would you most hang around with?",
responses: [{
text: 'Wonder Woman',
value: 'Superman'
},
{
text: 'Green Arrow',
value: 'The Flash'
},
{
text: 'Robin',
value: 'Batman'
}
]
},
{
id: 4,
text: "What's your favourite colour?",
responses: [{
text: 'Black',
value: 'Batman'
},
{
text: 'Red',
value: 'The Flash'
},
{
text: 'Blue',
value: 'Superman'
}
]
},
{
id: 5,
text: "When do you help people?",
responses: [{
text: 'Every chance I can',
value: 'The Flash'
},
{
text: 'At night',
value: 'Batman'
},
{
text: 'When they need me to',
value: 'Superman'
}
]
},
]
};
console.log(quiz.title)
},
data() {
return{
quiz: quiz,
questionIndex: 0,
userResponses: Array()
}
},
methods: {
// Go to next question
next() {
this.questionIndex++;
console.log(this.userResponses);
},
// Go to previous question
prev() {
this.questionIndex--;
},
score() {
//find the highest occurrence in responses
var modeMap = {};
var maxEl = this.userResponses[0],
maxCount = 1;
for (var i = 0; i < this.userResponses.length; i++) {
var el = this.userResponses[i];
if (modeMap[el] == null)
modeMap[el] = 1;
else
modeMap[el]++;
if (modeMap[el] > maxCount) {
maxEl = el;
maxCount = modeMap[el];
}
}
return maxEl;
}
}
},
}
</script>