30.09.2026 - INF - RandomGenen and ArrayShuffle - Cheap
This commit is contained in:
@@ -0,0 +1,101 @@
|
|||||||
|
let score = 0;
|
||||||
|
let currentQuestionIndex = 0;
|
||||||
|
|
||||||
|
let questions = [
|
||||||
|
{
|
||||||
|
question: "Was ist Event Bubbling?",
|
||||||
|
answers: ["Essen", "Trinken", "JavaScript-Verhalten"],
|
||||||
|
correctAnswer: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
question: "Wie groß ist Eiffelturm?",
|
||||||
|
answers: ["330 Meter", "289 Meter", "312 Meter"],
|
||||||
|
correctAnswer: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
question: "Wer hat JavaScript erstellt?",
|
||||||
|
answers: ["Brendan Eich", "Lebron James", "Lionel Messi"],
|
||||||
|
correctAnswer: 0,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
function shuffleArray(questionArray)
|
||||||
|
{
|
||||||
|
let newArray = [questionArray.length];
|
||||||
|
let rng = getRandomNumber(questionArray.length);
|
||||||
|
for(i = 0; i < questionArray.length; i++)
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
rng = getRandomNumber(questionArray.length);
|
||||||
|
}
|
||||||
|
while(newArray.includes(rng));
|
||||||
|
newArray[i] = rng;
|
||||||
|
}
|
||||||
|
|
||||||
|
newArray.forEach((value, index) => {
|
||||||
|
newArray[index] = questionArray[value];
|
||||||
|
});
|
||||||
|
|
||||||
|
return newArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRandomNumber(maxValue){
|
||||||
|
return Math.floor(Math.random() * maxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function resetQuiz(){
|
||||||
|
document.getElementById("result").hidden = true;
|
||||||
|
currentQuestionIndex = 0;
|
||||||
|
score = 0;
|
||||||
|
showQuestions();
|
||||||
|
document.getElementById("quiz-container").hidden = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function showQuestions() {
|
||||||
|
|
||||||
|
questions = shuffleArray(questions);
|
||||||
|
|
||||||
|
|
||||||
|
let btn = document.getElementById("resultBtn");
|
||||||
|
if (currentQuestionIndex >= questions.length) {
|
||||||
|
document.getElementById("quiz-container").hidden = true;
|
||||||
|
document.getElementById("result").hidden = false;
|
||||||
|
document.getElementById("score").textContent = score;
|
||||||
|
|
||||||
|
btn.hidden = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let question = questions[currentQuestionIndex];
|
||||||
|
|
||||||
|
|
||||||
|
document.getElementById("question").textContent = question.question;
|
||||||
|
|
||||||
|
let answersDiv = document.getElementById("answers");
|
||||||
|
answersDiv.innerHTML = "";
|
||||||
|
|
||||||
|
question.answers.forEach((answer, index ) => {
|
||||||
|
let answerBtn = document.createElement("button");
|
||||||
|
answerBtn.textContent = answer;
|
||||||
|
answerBtn.onclick = () => checkAnswer(index, question.correctAnswer);
|
||||||
|
answersDiv.appendChild(answerBtn);
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
function checkAnswer(selectedIndex, indexOfCorrectAnswer) {
|
||||||
|
|
||||||
|
/*const correctAnswer = questions[currentQuestionIndex].correctAnswer;*/
|
||||||
|
if(selectedIndex === indexOfCorrectAnswer){
|
||||||
|
score++;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentQuestionIndex++;
|
||||||
|
showQuestions();
|
||||||
|
}
|
||||||
|
|
||||||
|
showQuestions();
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<p>Die Kopfzeile wird beim Durschalten der Fragen nie verändert oder neu geladen.</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<div id="quiz-container">
|
||||||
|
<h2 id="question"></h2>
|
||||||
|
<div id="answers"></div>
|
||||||
|
<!--<button id="nextBtn" onclick="showQuestions()">Next Question</button> -->
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="result" hidden="true">
|
||||||
|
<h2>Quiz Beendet!</h2>
|
||||||
|
<p>Deine Punktzahl: <span id="score"></span></p>
|
||||||
|
<button id="resultBtn" hidden="true" onclick="resetQuiz()">Reset</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<footer>
|
||||||
|
<p>Dieser teil wird beim druschalten der Fragen nie verändert oder neu geladen.</p>
|
||||||
|
</footer>
|
||||||
|
<script src="assets/js/quiz.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user