284 lines
5.5 KiB
JavaScript
284 lines
5.5 KiB
JavaScript
let num1
|
||
let num2
|
||
let correctAnswer
|
||
let score=0
|
||
let currentMode=""
|
||
|
||
let gewaehlteModes=[]
|
||
let gewaehltesLevel=""
|
||
let gewaehltAnzahl=0
|
||
|
||
let aktFrage=0
|
||
let richtigAnzahl=0
|
||
let falschAnzahl=0
|
||
|
||
let timerZahl=15
|
||
let timerInterval
|
||
|
||
let gesperrt=false
|
||
|
||
function waehleMode(mode,btn){
|
||
|
||
let index=gewaehlteModes.indexOf(mode)
|
||
|
||
if(index==-1){
|
||
gewaehlteModes.push(mode)
|
||
btn.classList.add("aktiv")
|
||
}else{
|
||
gewaehlteModes.splice(index,1)
|
||
btn.classList.remove("aktiv")
|
||
}
|
||
|
||
document.getElementById("btn-mix").classList.remove("aktiv")
|
||
}
|
||
|
||
function waehleAlle(){
|
||
|
||
gewaehlteModes=["addition","subtraktion","multiplikation"]
|
||
|
||
document.getElementById("btn-addition").classList.add("aktiv")
|
||
document.getElementById("btn-subtraktion").classList.add("aktiv")
|
||
document.getElementById("btn-multiplikation").classList.add("aktiv")
|
||
document.getElementById("btn-mix").classList.add("aktiv")
|
||
|
||
}
|
||
|
||
function waehleLevel(level,btn){
|
||
|
||
gewaehltesLevel=level
|
||
|
||
let alleLevel=document.querySelectorAll(".level button")
|
||
|
||
for(let i=0;i<alleLevel.length;i++){
|
||
alleLevel[i].classList.remove("aktiv")
|
||
}
|
||
|
||
btn.classList.add("aktiv")
|
||
|
||
}
|
||
|
||
function waehleAnzahl(anzahl,btn){
|
||
|
||
gewaehltAnzahl=anzahl
|
||
|
||
let alleAnzahl=document.querySelectorAll(".anzahl button")
|
||
|
||
for(let i=0;i<alleAnzahl.length;i++){
|
||
alleAnzahl[i].classList.remove("aktiv")
|
||
}
|
||
|
||
btn.classList.add("aktiv")
|
||
|
||
}
|
||
|
||
function spielStarten(){
|
||
|
||
if(gewaehlteModes.length==0){
|
||
document.getElementById("fehler").textContent="Bitte eine Rechenart wählen"
|
||
return
|
||
}
|
||
|
||
if(gewaehltesLevel==""){
|
||
document.getElementById("fehler").textContent="Bitte eine Schwierigkeit wählen"
|
||
return
|
||
}
|
||
|
||
if(gewaehltAnzahl==0){
|
||
document.getElementById("fehler").textContent="Bitte Anzahl Fragen wählen"
|
||
return
|
||
}
|
||
|
||
score=0
|
||
aktFrage=0
|
||
richtigAnzahl=0
|
||
falschAnzahl=0
|
||
|
||
document.getElementById("score").textContent=0
|
||
|
||
document.getElementById("setup").style.display="none"
|
||
document.getElementById("spiel").style.display="block"
|
||
document.getElementById("endscreen").style.display="none"
|
||
|
||
generateQuestion()
|
||
|
||
}
|
||
|
||
function generateQuestion(){
|
||
|
||
gesperrt=false
|
||
|
||
aktFrage++
|
||
|
||
document.getElementById("fortschritt-text").textContent="Frage "+aktFrage+" von "+gewaehltAnzahl
|
||
|
||
let zufallIndex=Math.floor(Math.random()*gewaehlteModes.length)
|
||
|
||
currentMode=gewaehlteModes[zufallIndex]
|
||
|
||
let maxZahl=10
|
||
|
||
if(gewaehltesLevel=="mittel")maxZahl=50
|
||
if(gewaehltesLevel=="schwer")maxZahl=100
|
||
|
||
num1=Math.floor(Math.random()*maxZahl)+1
|
||
num2=Math.floor(Math.random()*maxZahl)+1
|
||
|
||
if(currentMode==="addition"){
|
||
correctAnswer=num1+num2
|
||
document.getElementById("question").textContent=num1+" + "+num2+" = ?"
|
||
}
|
||
|
||
if(currentMode==="subtraktion"){
|
||
|
||
if(num2>num1){
|
||
let temp=num1
|
||
num1=num2
|
||
num2=temp
|
||
}
|
||
|
||
correctAnswer=num1-num2
|
||
document.getElementById("question").textContent=num1+" - "+num2+" = ?"
|
||
}
|
||
|
||
if(currentMode==="multiplikation"){
|
||
correctAnswer=num1*num2
|
||
document.getElementById("question").textContent=num1+" × "+num2+" = ?"
|
||
}
|
||
|
||
document.getElementById("answer").value=""
|
||
document.getElementById("result").textContent=""
|
||
document.getElementById("answer").focus()
|
||
|
||
timerStarten()
|
||
|
||
}
|
||
|
||
function timerStarten(){
|
||
|
||
clearInterval(timerInterval)
|
||
|
||
timerZahl=15
|
||
|
||
document.getElementById("timer").textContent="Zeit: "+timerZahl
|
||
document.getElementById("timer").style.color="green"
|
||
|
||
timerInterval=setInterval(function(){
|
||
|
||
timerZahl--
|
||
|
||
document.getElementById("timer").textContent="Zeit: "+timerZahl
|
||
|
||
if(timerZahl<=5){
|
||
document.getElementById("timer").style.color="red"
|
||
}
|
||
|
||
if(timerZahl<=0){
|
||
clearInterval(timerInterval)
|
||
zeitAbgelaufen()
|
||
}
|
||
|
||
},1000)
|
||
|
||
}
|
||
|
||
function zeitAbgelaufen(){
|
||
|
||
if(gesperrt)return
|
||
|
||
gesperrt=true
|
||
|
||
falschAnzahl++
|
||
|
||
document.getElementById("result").textContent="Zeit abgelaufen! Richtig wäre: "+correctAnswer
|
||
document.getElementById("result").style.color="orange"
|
||
|
||
setTimeout(naechsteOderEnde,2000)
|
||
|
||
}
|
||
|
||
function checkAnswer(){
|
||
|
||
if(gesperrt)return
|
||
|
||
let userAnswer=Number(document.getElementById("answer").value)
|
||
|
||
if(document.getElementById("answer").value=="")return
|
||
|
||
clearInterval(timerInterval)
|
||
|
||
gesperrt=true
|
||
|
||
let resultText=document.getElementById("result")
|
||
|
||
if(userAnswer===correctAnswer){
|
||
|
||
resultText.textContent="Richtig! Antwort: "+correctAnswer
|
||
resultText.style.color="green"
|
||
|
||
score++
|
||
richtigAnzahl++
|
||
|
||
document.getElementById("score").textContent=score
|
||
|
||
}else{
|
||
|
||
resultText.textContent="Falsch! Richtige Antwort: "+correctAnswer
|
||
resultText.style.color="red"
|
||
|
||
falschAnzahl++
|
||
|
||
}
|
||
|
||
setTimeout(naechsteOderEnde,2000)
|
||
|
||
}
|
||
|
||
function naechsteOderEnde(){
|
||
|
||
if(aktFrage>=gewaehltAnzahl){
|
||
spielEnde()
|
||
}else{
|
||
generateQuestion()
|
||
}
|
||
|
||
}
|
||
|
||
function spielEnde(){
|
||
|
||
clearInterval(timerInterval)
|
||
|
||
document.getElementById("spiel").style.display="none"
|
||
document.getElementById("endscreen").style.display="block"
|
||
|
||
let prozent=Math.round((richtigAnzahl/gewaehltAnzahl)*100)
|
||
|
||
document.getElementById("prozent").textContent=prozent+"%"
|
||
document.getElementById("end-richtig").textContent=richtigAnzahl
|
||
document.getElementById("end-falsch").textContent=falschAnzahl
|
||
document.getElementById("end-text").textContent="Du hast "+richtigAnzahl+" von "+gewaehltAnzahl+" richtig"
|
||
|
||
if(prozent==100){
|
||
document.getElementById("end-titel").textContent="Perfekt"
|
||
document.getElementById("prozent").style.color="gold"
|
||
}
|
||
else if(prozent>=70){
|
||
document.getElementById("end-titel").textContent="Sehr gut"
|
||
document.getElementById("prozent").style.color="green"
|
||
}
|
||
else if(prozent>=50){
|
||
document.getElementById("end-titel").textContent="Gut gemacht"
|
||
document.getElementById("prozent").style.color="orange"
|
||
}
|
||
else{
|
||
document.getElementById("end-titel").textContent="Weiter üben"
|
||
document.getElementById("prozent").style.color="red"
|
||
}
|
||
|
||
}
|
||
|
||
function nochmal(){
|
||
|
||
document.getElementById("endscreen").style.display="none"
|
||
document.getElementById("setup").style.display="block"
|
||
|
||
} |