function - Triggering a "Win" in a Vanilla JavaScript Hangman Game -


so i've asked few questions regarding hangman game (and have gotten awesome answers), i've been continually tripped "score keeper". game need too, "win" not logged until after random key pressed after entire word has been filled in...

<!doctype html> <html> <head>     <title>hangman</title> </head> <body>     <h1>hangman!</h1>      <p>         <font size="+3"><span id="answer"></span></font>     </p>     <p>lives: <span id="counter"></span></p>     <p id="wrongguesses"></p>     <p>wins: <span id="wins"></span></p>     <p>losses: <span id="losses"></span></p>       <script type="text/javascript">  var word;  var guess;      //user guess var letters = [];       //correctly guessed letters var wrongletters = [];      //incorrectly guessed letters var counter;        //counts correct letters var losses = 0; var wins = 0;   document.getelementbyid("losses").innerhtml = losses; document.getelementbyid("wins").innerhtml = wins;  var wordlist = ["cat", "dog", "wolf", "laser", "apple"];        //fill list later!!   //randomly chooses word array , replaces letters underscores function start() {     word = wordlist[math.floor(math.random() * wordlist.length)];     counter = 7;     document.getelementbyid("counter").innerhtml = counter;     (i = 0; < word.length; i++) {         letters[i] = "__";     }      document.getelementbyid("answer").innerhtml = letters.join(" ");     console.log(word);  }   //checks if letter in word or not function checkletter() {     document.onkeyup = function(event) {         guess = event.key.tolowercase();         //var found = false;         (i = 0; < word.length; i++) {             if (guess === word[i]) {                 letters[i] = guess;                 document.getelementbyid("answer").innerhtml = letters.join(" ");                 //found = true;             }          }         //wrong letters go wrongletters array , displayed         //if (found) return;         if (wrongletters.indexof(guess) < 0) {             wrongletters.push(guess);             document.getelementbyid("wrongguesses").innerhtml = wrongletters.join(" ");             //every wrong guess subtracts 1 counter             counter--;             console.log(counter);             document.getelementbyid("counter").innerhtml = counter;             //when counter reaches 0 it's game on             //+1 losses if 7 words missed              if (counter > 0 && letters.join("") === word) {             //the issue                 document.getelementbyid("wins").innerhtml = wins + 1;                 console.log(wins);                 confirm("you win! play again?");                     wins++;                     counter = 7;                     letters = [ ];                     wrongletters = [ ];                 start();             }              else if (counter === 0) {                 document.getelementbyid("losses").innerhtml = losses + 1;                 console.log(losses);                 confirm("you loose... play again?"); {                     losses++;                     counter = 7;                     letters = [];                     wrongletters = [];                     start();                 }             }         }     } }   start(); checkletter();  </script> </body> </html> 

here's have far , here jsfiddle link: https://jsfiddle.net/t57zfv3t/11/ know main issue found bool , return, can't seem find way work around it. if has insight appreciated , apologize in advanced have bothered this. thanks!!

you return if letter found if it's whole word match, check in condition too

//wrong letters go wrongletters array , displayed if (found && letters.join("") !== word) return; 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -