jeudi 19 septembre 2019

javascrpit - if-statement doesn't work. doesn't allign number to variable

Hello people of stackoverflow,

I am having a problem, where the console doens't print out "geschafft", like i wanted it to. I wanted to build an Website, wehere it counts, how fast you typed numbers so i need to check if the number typed by the user equals the given number. The Code snipped that doesn't work is:

var gedrueckt = "";
var zahl = "";
var spielerZahl = '';
//erzeugen der Zufallszahl
var min = 0;
var max = 9;
var zahl = Math.round(Math.random() * (max - min)) + min;
console.log(zahl);
//Zahl in HTML ausgeben
document.getElementById("zutippen").innerHTML = zahl;

//Erkennung der Taste
window.addEventListener("keydown", TasteGedrueckt,);
function TasteGedrueckt (evt) {
     gedrueckt = evt.key;
        if (gedrueckt == 1){
            spielerZahl = 1;
        }
        else if (gedrueckt == 2){
            spielerZahl = 2;
        }
        else if (gedrueckt == 3){
            spielerZahl = 3;
        }
        else if (gedrueckt == 4){
            spielerZahl = 4;
        }
        else if (gedrueckt == 5){
            spielerZahl = 5;
        }
        else if (gedrueckt == 6){
            spielerZahl = 6;
        }
        else if (gedrueckt == 7){
            spielerZahl = 7;
        }
        else if (gedrueckt == 8){
            spielerZahl = 8;
        }
        else if (gedrueckt == 9){
            spielerZahl = 9;
        }
        else if (gedrueckt == 0){
            spielerZahl = 0;
        }
        console.log(spielerZahl);
}

//Überprüfung ob zahl gleich
if (zahl == spielerZahl){
    console.log("geschafft!");
}

Sorry for my bad english, I hope someone can help my. Googled for three days, also tried parseInt and nothing seems to work..

Aucun commentaire:

Enregistrer un commentaire