Facebook
From soham, 3 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 49
  1. var programming_languages = [
  2.         "python",
  3.         "javascript",
  4.         "mongodb",
  5.         "json",
  6.         "java",
  7.         "html",
  8.         "css",
  9.         "c",
  10.         "csharp",
  11.         "golang",
  12.         "kotlin",
  13.         "php",
  14.         "sql",
  15.         "ruby"
  16. ]
  17.  
  18. let answer = '';
  19. let maxWrong = 6;
  20. let mistakes = 0;
  21. let guessed = [];
  22. let wordStatus = null;
  23.  
  24. function randomWord() {
  25.         var randomLanguageSelector = Math.floor(Math.random() * programming_languages.length) ;
  26.         answer = programming_languages[randomLanguageSelector];
  27.         console.log("Random programming language selected "+ answer + " random number "+ randomLanguageSelector);
  28. }
  29.  
  30. function generateButtons() {
  31.        
  32.         let buttonsArray = "qwertyuiopasdfghjklzxcvbnm".split("");
  33.        
  34.         console.log("Total letters in array " + buttonsArray.length);
  35.        
  36.         var buttonString = "";
  37.         var i = 0 ;
  38.        
  39.         for( i = 0 ;  i < buttonsArray.length ; i++){
  40.                 buttonString = buttonString + `<button class = "btn btn-lg btn-primary m-2"
  41.                 onclick = "handleGuess('`+buttonsArray[i]+`');">`+buttonsArray[i]+`</button>`;
  42.         }
  43.        
  44.        
  45.         document.getElementById("keyboard").innerHTML = buttonString;
  46. }
  47.  
  48. function handleGuess(chosenLetter) {
  49.  
  50.         if(guessed.indexOf(chosenLetter) == -1){
  51.                 guessed.push(chosenLetter);
  52.                 console.log("Guessed letters "+ guessed);
  53.         }      
  54.  
  55.  
  56.   mistakes++;
  57.   updateHangmanPicture();
  58.   checkIfGameLost();
  59. }
  60.  
  61. function updateHangmanPicture() {
  62.   document.getElementById("hangmanPic").src = "./images/"+ mistakes + ".jpg";
  63. }
  64.  
  65. function checkIfGameWon() {
  66.  
  67. }
  68.  
  69. function checkIfGameLost() {
  70.         if(mistakes === maxWrong) {
  71.                 document.getElementById('wordSpotlight').innerHTML = 'the answer was : '+ answer;
  72.                 document.getElementById('keyboard').innerHTML = 'You Lost!!!';
  73.         }
  74.  
  75. }
  76.  
  77. function guessedWord() {
  78.         var currentAnswer = answer.split("") ;
  79.         var i = 0 ;
  80.         for(var i = 0 ; i < currentAnswer.length ; i++){
  81.                 console.log("Current Letter "+currentAnswer[i]);
  82.         }
  83.         document.getElementById('wordSpotlight').innerHtml ="";
  84. }
  85. function updateMistakes() {
  86.  
  87. }
  88.  
  89. function reset() {
  90.  
  91. }
  92.  
  93. document.getElementById('maxWrong').innerHTML = maxWrong;
  94.  
  95. randomWord();
  96. generateButtons();
  97. guessedWord();