Commit ae51fa7f authored by Ferdinand Schimana's avatar Ferdinand Schimana
Browse files

5sec b4 suicide

parent 38edc38b
......@@ -8,11 +8,11 @@
<body>
<h1>Frontend WS2020-Js Quiz</h1>
<h3 id="home">Click <a href="">here</a> to start the quiz!</h3>
<h3 id="quiz">Click <a href="">here</a> to home!</h3>
<h3 id="goQuiz">Click <a href="">here</a> to start the quiz!</h3>
<h3 id="goHome">Click <a href="">here</a> to home!</h3>
<div id="content">
<div id="question"></div>
<div id="answer"></div>
</div>
<script src="index.js" type="module"></script>
</body>
......
// index.js
import { getQuestions } from './questions.js'
import {askQuestion, answerQuestion} from './quiz.js'
window.addEventListener('load', function () {
const onRender = () =>{
const onRender = async (id) =>{
const {pathname} = window.location
if(pathname==='/'){
let hide=document.getElementById('quiz')
let nothide=document.getElementById('home')
let hide=document.getElementById('goHome')
let nothide=document.getElementById('goQuiz')
hide.style.display="none";
nothide.style.display="initial";
document.getElementById('question').innerHTML=''
document.getElementById('answer').innerHTML=''
}else if(pathname==='/quiz'){
displayQuiz()
let hide=document.getElementById('home')
window.question = await askQuestion()
console.log(window.question)
displayQuiz(window.question)
let hide=document.getElementById('goQuiz')
hide.style.display="none";
}else if(pathname==='/result'){
document.getElementById('content').innerHTML='result'
}else if(pathname==='/result' && id != undefined){
let nothide=document.getElementById('goHome')
nothide.style.display="initial";
}
else{
document.getElementById('content').innerHTML='not found'
......@@ -27,23 +30,20 @@ window.addEventListener('load', function () {
Array.from(document.querySelectorAll('a'))[0].addEventListener('click',(evt) => {
evt.preventDefault()
history.pushState({}, 'quiz', '/quiz')
onRender()
onRender("")
})
Array.from(document.querySelectorAll('a'))[1].addEventListener('click',(evt) => {
evt.preventDefault()
history.pushState({}, 'home', '/')
onRender()
onRender("")
})
Array.from(document.getElementsByClassName('answer')).forEach(
function (item){item.addEventListener('click',(evt) => {
console.log("hey")
document.getElementById('answer').addEventListener('click', (evt) => {
evt.preventDefault()
history.pushState({}, 'result', '/result')
onRender()
})})
onRender(evt.target.id)
displayAnswer(window.question, evt.target.id)
})
})
function createQuizItem(content,num) {
let p = document.createElement('a');
p.setAttribute("href", "");
......@@ -57,16 +57,22 @@ function createQuestion(content) {
p.textContent = content;
return p;
}
async function displayQuiz(){
const question = await askQuestion()
document.getElementById('content').appendChild(createQuestion(question.question))
document.getElementById('content').appendChild(createQuizItem('a: '+question.a,'a'))
document.getElementById('content').appendChild(createQuizItem('b: '+question.b,'b'))
document.getElementById('content').appendChild(createQuizItem('c: '+question.c,'c'))
document.getElementById('content').appendChild(createQuizItem('d: '+question.d,'d'))
/* let a= await prompt("Enter answer") */
/* const answer = await answerQuestion(question, a)
console.log(answer ? 'correct' : 'incorrect') */
async function displayQuiz(question){
document.getElementById('question').appendChild(createQuestion(question.question))
document.getElementById('answer').appendChild(createQuizItem('a: '+question.a,'a'))
document.getElementById('answer').appendChild(createQuizItem('b: '+question.b,'b'))
document.getElementById('answer').appendChild(createQuizItem('c: '+question.c,'c'))
document.getElementById('answer').appendChild(createQuizItem('d: '+question.d,'d'))
}
async function displayAnswer(question, answer){
console.log(question)
const ans = await answerQuestion(question, answer)
if(ans === true){
document.getElementById('question').innerHTML='Correct Answer'
document.getElementById('answer').innerHTML=''
}else{
document.getElementById('question').innerHTML='Wrong Answer'
document.getElementById('answer').innerHTML=''
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment