├── .gitattributes ├── images ├── 0.jpg ├── 1.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── 5.jpg └── 6.jpg ├── index.html ├── LICENSE └── js └── hangman.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /images/0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonjsuh/Vanilla-Javascript-Hangman-Game/HEAD/images/0.jpg -------------------------------------------------------------------------------- /images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonjsuh/Vanilla-Javascript-Hangman-Game/HEAD/images/1.jpg -------------------------------------------------------------------------------- /images/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonjsuh/Vanilla-Javascript-Hangman-Game/HEAD/images/2.jpg -------------------------------------------------------------------------------- /images/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonjsuh/Vanilla-Javascript-Hangman-Game/HEAD/images/3.jpg -------------------------------------------------------------------------------- /images/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonjsuh/Vanilla-Javascript-Hangman-Game/HEAD/images/4.jpg -------------------------------------------------------------------------------- /images/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonjsuh/Vanilla-Javascript-Hangman-Game/HEAD/images/5.jpg -------------------------------------------------------------------------------- /images/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonjsuh/Vanilla-Javascript-Hangman-Game/HEAD/images/6.jpg -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Hangman 12 | 13 | 14 |
15 |

Hangman

16 |
Wrong Guesses: 0 of
17 |
18 | 19 |

Guess the Programming Language:

20 |

The word to be guessed goes here

21 |
22 | 23 |
24 |
25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 simonjsuh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /js/hangman.js: -------------------------------------------------------------------------------- 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 | answer = programming_languages[Math.floor(Math.random() * programming_languages.length)]; 26 | } 27 | 28 | function generateButtons() { 29 | let buttonsHTML = 'abcdefghijklmnopqrstuvwxyz'.split('').map(letter => 30 | ` 31 | 38 | `).join(''); 39 | 40 | document.getElementById('keyboard').innerHTML = buttonsHTML; 41 | } 42 | 43 | function handleGuess(chosenLetter) { 44 | guessed.indexOf(chosenLetter) === -1 ? guessed.push(chosenLetter) : null; 45 | document.getElementById(chosenLetter).setAttribute('disabled', true); 46 | 47 | if (answer.indexOf(chosenLetter) >= 0) { 48 | guessedWord(); 49 | checkIfGameWon(); 50 | } else if (answer.indexOf(chosenLetter) === -1) { 51 | mistakes++; 52 | updateMistakes(); 53 | checkIfGameLost(); 54 | updateHangmanPicture(); 55 | } 56 | } 57 | 58 | function updateHangmanPicture() { 59 | document.getElementById('hangmanPic').src = './images/' + mistakes + '.jpg'; 60 | } 61 | 62 | function checkIfGameWon() { 63 | if (wordStatus === answer) { 64 | document.getElementById('keyboard').innerHTML = 'You Won!!!'; 65 | } 66 | } 67 | 68 | function checkIfGameLost() { 69 | if (mistakes === maxWrong) { 70 | document.getElementById('wordSpotlight').innerHTML = 'The answer was: ' + answer; 71 | document.getElementById('keyboard').innerHTML = 'You Lost!!!'; 72 | } 73 | } 74 | 75 | function guessedWord() { 76 | wordStatus = answer.split('').map(letter => (guessed.indexOf(letter) >= 0 ? letter : " _ ")).join(''); 77 | 78 | document.getElementById('wordSpotlight').innerHTML = wordStatus; 79 | } 80 | 81 | function updateMistakes() { 82 | document.getElementById('mistakes').innerHTML = mistakes; 83 | } 84 | 85 | function reset() { 86 | mistakes = 0; 87 | guessed = []; 88 | document.getElementById('hangmanPic').src = './images/0.jpg'; 89 | 90 | randomWord(); 91 | guessedWord(); 92 | updateMistakes(); 93 | generateButtons(); 94 | } 95 | 96 | document.getElementById('maxWrong').innerHTML = maxWrong; 97 | 98 | randomWord(); 99 | generateButtons(); 100 | guessedWord(); 101 | --------------------------------------------------------------------------------