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();
|
||||
Reference in New Issue
Block a user