-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (59 loc) · 1.89 KB
/
script.js
File metadata and controls
66 lines (59 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var g = 0;
var words = [
"penguin", "hangman", "universe", "treehouse", "vacation", "inside", "yellow",
"school", "everybody", "clock", "book", "animal", "human", "overall",
"rainbow", "fireplace", "pumpkin", "dogtoy", "waterfall", "slippers", "weasel",
"beanbag", "rocketship", "family", "hamster", "light", "scared", "intergalactic",
"tomorrow", "recces", "vacine", "covid19", "magical"
];
var word = words[Math.floor(Math.random() * words.length)];
var answerArray = Array(word.length).fill("_");
var remainingLetters = word.length;
console.log("Welcome to Hangman! Type `play()` to start playing.");
function play() {
while (remainingLetters > 0) {
let guess = prompt("Guess a letter (or full word), or type 'hint'.\n" + answerArray.join(" "));
if (guess === null) {
if (confirm("Are you sure you want to quit?")) {
alert("Thanks for playing!");
return;
} else {
continue;
}
}
guess = guess.toLowerCase();
if (guess === "hint") {
g++;
let revealed = false;
while (!revealed) {
let tr = word[Math.floor(Math.random() * word.length)];
if (!answerArray.includes(tr)) {
alert("Hint letter: " + tr);
revealed = true;
}
}
} else if (guess === word) {
alert("Correct! You guessed the word.");
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
continue;
} else {
g++;
let correctGuess = false;
for (let j = 0; j < word.length; j++) {
if (word[j] === guess && answerArray[j] === "_") {
answerArray[j] = guess;
remainingLetters--;
correctGuess = true;
}
}
if (!correctGuess) {
alert("Incorrect guess.");
}
}
}
alert("Word: " + answerArray.join(" "));
alert("Guesses: " + g);
alert("Game over! The word was: " + word);
}