-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
132 lines (103 loc) · 3.62 KB
/
app.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
var qwerty = document.querySelector('#qwerty');
var phrase = document.getElementById('phrase');
var startButton = document.getElementById('overlay');
var startButtonLink = document.getElementsByClassName('btn__reset');
//---------------------------Miss counter
var missed = 0;
//---------------------------Phrase array
var phrases = [
'hello there',
'html is easy',
'code is fun',
'javascript is tough',
'css is cool'
];
//---------------------------------------Functions
function getRandomPhraseAsArray(arr) {
var i = Math.floor(Math.random() * arr.length);
var letterArray = arr[i].split("");
return letterArray;
}
//-------------------------------------------------------------
function addPhrasetoDisplay(letterarr) {
for (var i = 0; i < letterarr.length; i++) {
var li = document.createElement('li');
var letter = document.createTextNode(letterarr[i]);
li.appendChild(letter);
//------------------Check is a space, then add the right class
if ( li.innerText.indexOf(' ') >= 0) {
li.className = 'space';
document.getElementsByTagName('ul')[0].appendChild(li);
//----------------------------------------------------------
} else {
li.className = 'letter';
document.getElementsByTagName('ul')[0].appendChild(li);
}
}
}
const phraseArray = getRandomPhraseAsArray(phrases);
addPhrasetoDisplay(phraseArray);
//---------------------------------------------------------------
//---------------------------------------------------------------------------------------
function checkLetter(input) {
var letter = document.getElementsByClassName('letter');
var letterFound = 0;
var correct = false;
for (var i = 0; i < letter.length ; i++) {
if ( input == letter[i].innerText ) {
letter[i].className += ' show';
letterFound += 1;
correct = true;
if ( i == letter.length) {
return correct;
}
}
//-----------------------------------------------------
else if ( i == letter.length - 1 && correct == true ) {
return correct;
}
//---------------------------------------------------------
else if ( i == letter.length - 1 && correct == false ) {
return null;
}
}
};
//---------------------------------------------------------------------------------------
qwerty.addEventListener("click", function(e) {
const target = e.target;
//----------------------------------------
if ( target.nodeName == 'BUTTON' ) {
target.className += ' chosen';
target.disabled = true;
//----------------------------------------
if ( checkLetter(target.innerText) === null ) {
var scoreboard = document.getElementsByTagName('ol');
var tries = document.getElementsByClassName('tries');
scoreboard[0].removeChild(tries[0]);
missed += 1;
checkWin();
//-----------------------------------------------------------------------
} else {
checkWin();
}
}
});
function checkWin() {
var show = document.getElementsByClassName('show');
var letter = document.getElementsByClassName('letter');
//--------------------------------------------------------------------
if (show.length == letter.length) {
startButton.style.display = 'flex';
startButton.className = 'win';
startButtonLink[0].innerText = 'You Won!'
//-------------------------------------------------------------------
} else if (missed >= 5 ) {
startButton.style.display = 'flex';
startButton.className = 'lose';
startButtonLink[0].innerText = 'You Lost!';
}
}
//----------------------------Overlay hide
startButton.addEventListener("click", function() {
startButton.style.display = 'none';
})