Please explain to an inexperienced coder why doesn't this damn code...(?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
23 views
Hello, I can not understand why this code does not work, in my understanding, it should take pressing, record in a variable keyName value (e.g. "s") and compare, therefore in the event if you enter "s" should display alert. But it turns out that keyName = undefined, even if I press a key on the keyboard.

let symbolsCounter = 0; let mistakesCounter = 0; function checkUsersKey (levelText) { var theEnd = false; var counterLetters = 0; var keyName; while (theEnd != true ) { document.addEventListener("keypress", (event) => { keyName = event.key; }) if(levelText[counterLetters] == keyName){ alert("true"); } theEnd = true; } } checkUsersKey("something");
by | 23 views

3 Answers

0 like 0 dislike
Old answer
let symbolsCounter = 0; let mistakesCounter = 0; function checkUsersKey (levelText) { var theEnd = false; var counterLetters = 0; var keyName; while (theEnd != true ) { document.addEventListener("keypress", (event) => { keyName = event.key; if(levelText[counterLetters] == keyName){ alert("true"); } }) theEnd = true; } } checkUsersKey("something");

by
0 like 0 dislike
It is not clear exactly how you want it to work the code, but now it works according to what you wrote.

You have written a function and call it once. In that moment, when you it is done, the variable keyName is really still nothing.
In order for your function performed the check after the key is pressed, you must add it to the function that is executed at the touch of a button.

var keyName; var counterLetters = 0; document.addEventListener("keypress", (event) => { keyName = event.key; checkUsersKey("something"); }); function checkUsersKey (levelText) { if(levelText[counterLetters] == keyName){ alert("true"); } }
by
0 like 0 dislike
There is such a thing, your code took clicked, but event.key is not available for a direct link, in other scopes, except the one where he was raised, so it is necessary to make reference to the link (hehe), or processed on site. From the code below shows how to do it.
var i=0;var j=0;window.mistake = 2;var guess = 0; var kN=0;var letter = ['s','g','f','v','p','t','m','n','b','l','d']; function checkUsersKey (levelText) {console.log("press any key"); var theEnd = false; var counterLetters; var keyName=0; while (theEnd != true ) { document.addEventListener("keypress", (event) => { keyName = event.key; kN =keyName; if("y"==keyName){window.mistake = 0} else{window.mistake = 1;} }) if(window.mistake==1){ i=i+1;console.log(kN+" no guess "+i+" tyme"); window.mistake=2} if(mistake==0){ j=j+1;console.log(kN+" guess "+j+" tyme");window.mistake=2}; theEnd = true; }; } var link = checkUsersKey;setInterval(function(){ return checkUsersKey(letter[j])},500);
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
0 comments
40,796 users