Witam, staram się zrobić quiz za pomocą javascript, potrzebował bym pomocy z walidacja odpowiedzi, gdy użytkownik wpisuje poprawna odpowiedź powinien pokazać alert że wpisał poprawną odpowiedź i kod powinien przejść do kolejnej odpowiedz, niestety kod który napisał tego nie robi
Byłbym wdzięczny za sugestie jak do tego się zabrać oraz za wszelkie wskazówki dotyczące kodu i jak go ulepszyć
Dziękuje i pozdrawiam, poniżej załączam kod html css i javascript oraz link do codepen
https://codepen.io/aleksandergale2/pen/BaYrbqg
let userinputName = prompt('Please enter your name');
let result = userinputName.toUpperCase();
document.getElementById("name").innerHTML = result;
const startButton = document.getElementById('btn')
const nextButton = document.getElementById('nextBtn')
let shuffleQuestions, currentQuestionIndex
const QuestionContainerElement = document.getElementById('question-container')
const paragraph= document.getElementById('welcome')
const questionElement = document.getElementById('question')
startButton.addEventListener('click', startGame)
function startGame(){
console.log("started")
startButton.classList.add('hide')
nextButton.classList.remove('hide')
shuffleQuestions = questions.sort(() => Math.random() - .5)
currentQuestionIndex = 0
showQuestion(shuffleQuestions[currentQuestionIndex])
QuestionContainerElement.classList.remove('hide')
paragraph.classList.add('hide')
}
nextButton.addEventListener('click', () => {
currentQuestionIndex++
setNextQuestion()
})
function setNextQuestion(){
showQuestion(shuffleQuestions[currentQuestionIndex])
if (shuffleQuestions.length > currentQuestionIndex + 1) {
nextButton.classList.remove('hide')
} else {
startButton.innerText = 'Restart'
startButton.classList.remove('hide')
nextButton.classList.add('hide')
}
}
function showQuestion(question){
let input=document.getElementById('textbox').value;
questionElement.innerText = question.question
if(input == questions.answer) {
alert("Correct answer");
}
}
const questions = [
{
question: 'What is the capital of france?',
answer : "paris"
},
{
question: 'What is the capital of wales?',
answer: "Cardif"
},
{
question: 'what is the capital of Italy?',
answer: "Rome"
},
{
question: 'What is the capital of Scotland',
answer: "Edinburgh"
}
]
#container {
width: 500px;
margin: 10rem auto;
padding: 1rem;
font-family: "Gill Sans MT";
font-size: 2rem;
text-align: center;
color: #fff;
background-color: rgb(42, 161, 240);
}
button {
border: none;
border-radius: 10px;
padding: 1rem;
color: #fff;
font-weight: 900;
cursor: pointer;
background-color: rgb(178, 81, 202);
}
.hide{
display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HNC QUIZ ASSESSMENT TEMPLATE</title>
<link rel="stylesheet" href="css/quiz_assess_styles.css"> </head>
<body>
<div id="container">
<p id="welcome">WELCOME TO THE QUIZ <span id="name"> ...name here... </span></p>
<div id="question-container" class="hide">
<div id="question">Question</div>
<input id="textbox" type="text" name="answer">
</div>
<button id="btn">START QUIZ</button>
<button id="nextBtn" class="hide">Next Question</button>
</div>
<script src="js/quiz_assess_script.js"></script>
</body>
</html>