-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
72 lines (60 loc) · 2.04 KB
/
custom.js
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
67
68
69
70
71
72
let word
let guessation
let guessesRemaining = 7
let guessIndex = 0
const fileNames = ["noman", "head", "torso", "leftleg", "rightleg", "leftarm", "rightarm", "dead"]
let excludedLetters = []
function revealpartofword() {
let newWord = word
newWord.split("").forEach(letter => {
if (!excludedLetters.includes(letter)) {
newWord=newWord.replaceAll(letter, "_")
}
})
guessation=newWord.split("").join(" ")
document.querySelector("h1").innerText=guessation
}
word=atob(location.href.split("?")[1])
revealpartofword()
let usedKeys = []
window.addEventListener("keypress", (e) => {
const key = e.key.toLowerCase()
if (!/^[a-z]+$/.test(key)) return
if (usedKeys.indexOf(key) != -1) {
document.querySelector("img").src="hangman/"+fileNames[guessIndex]+".png"
document.querySelector("p").innerText=key+" has already been guessed"
document.querySelector("p").style.color='black'
document.querySelector("p").style.display='block'
setTimeout(function(){
document.querySelector("p").style.display='none'
}, 1000)
return
}
usedKeys.push(e.key)
if (word.split("").indexOf(key) != -1) {
document.querySelector("p").innerText=key+" is in the word"
document.querySelector("p").style.color='limegreen'
document.querySelector("p").style.display='block'
setTimeout(function(){
document.querySelector("p").style.display='none'
}, 1000)
excludedLetters.push(key)
} else {
guessesRemaining--
guessIndex++
document.querySelector("img").src="hangman/"+fileNames[guessIndex]+".png"
document.querySelector("p").innerText=key+" is not in the word. "+guessesRemaining+" guesses remaining"
document.querySelector("p").style.color='rgb(255,83,83)'
document.querySelector("p").style.display='block'
setTimeout(function(){
document.querySelector("p").style.display='none'
}, 1000)
if (guessesRemaining == 0) {
location.href="lose.html?"+word
}
}
revealpartofword()
if (guessation.split(" ").join("")==word) {
location.href="win.html?"+word
}
})