{
var times=8
var word="pretty"
var outputWord="p r e t t y"
var blankSpaces="_ _ _ _ _ _"
var remains= word.length
var answerArray=[]
var roundWin=0
const form = html`
<form onsubmit="return false;">
<div>Input letter here: </div>
<div>
<input type=text name=inputText placeholder="input 1 lower-cased character at a time"></input>
</div>
<div>
<button name=OK>OK</button>
<button name=Reset>Reset</button>
</div>
<div>This is the current word: </div>
<input type=text name=current value="_ _ _ _ _ _"></input>
<div> Guessing times remained: </div>
<input type=text name=time value=8></input>
<div>Status: </div>
<div>
<input type=text name=hint></input>
</div>
</form>`
form.Reset.onclick = () => {
times=8
answerArray=[]
roundWin=0
remains= word.length
form.inputText.value=""
form.current.value=blankSpaces
form.hint.value=""
form.time.value=8
}
form.OK.onclick = () => {
if(times<=0||remains==0){return 0}
else{
var input=form.inputText.value
if (input.length != 1) {
form.hint.value=("Please enter a single letter!")
return 0
}
else if (input.charCodeAt()<97 || input.charCodeAt()>122){
form.hint.value=("Please enter a lower-cased letter!")
return 0
}
else {
roundWin=0
for (var j = 0; j < word.length; j++) {
if (word[j] == input && answerArray[j] != input) {
answerArray[j] = input
remains--
roundWin=1
}
}
var outputAnswer=""
for (var i = 0; i < outputWord.length; i++) {
var x = outputWord.charAt(i);
if (x != " ") {
if(answerArray[i/2]==undefined){outputAnswer= outputAnswer+ "_"}
else{outputAnswer=outputAnswer+ answerArray[i/2]}
}
else {
outputAnswer= outputAnswer+ " "
}
}
form.current.value=outputAnswer
times--
form.time.value=times
if (remains>0){
if(times>0){
if(roundWin==0){
form.hint.value=("Please try another letter.")
return 0
}
else{
form.hint.value=("")
return 0
}
}
else{
form.time.value=0
form.hint.value=("Game over! You lose!")
form.current.value=outputWord
return 0
}}
else{form.hint.value=("Congratulations! You win!")}
}}
}
return form
}