forked from rithmschool/guess-the-password-assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (76 loc) · 2.87 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
document.addEventListener('DOMContentLoaded', function() {
var wordCount = 10;
var guessCount = 4;
var password = '';
var start = document.getElementById('start');
start.addEventListener('click', function() {
toggleClasses(document.getElementById('start-screen'), 'hide', 'show');
toggleClasses(document.getElementById('game-screen'), 'hide', 'show');
startGame();
});
function toggleClasses(element) {
for (var i = 1; i < arguments.length; i++) {
element.classList.toggle(arguments[i]);
}
}
function startGame() {
// get random words and append them to the DOM
var wordList = document.getElementById("word-list");
var randomWords = getRandomValues(words, wordCount);
randomWords.forEach(function(word) {
var li = document.createElement("li");
li.innerText = word;
wordList.appendChild(li);
});
// set a secret password and the guess count display
password = getRandomValues(randomWords, 1)[0];
setGuessCount(guessCount);
// add update listener for clicking on a word
wordList.addEventListener('click', updateGame);
}
function getRandomValues(array, numberOfVals) {
return shuffle(array).slice(0, numberOfVals);
}
function shuffle(array) {
var arrayCopy = array.slice();
for (var idx1 = arrayCopy.length - 1; idx1 > 0; idx1--) {
// generate a random index between 0 and idx1 (inclusive)
var idx2 = Math.floor(Math.random() * (idx1 + 1));
// swap elements at idx1 and idx2
var temp = arrayCopy[idx1];
arrayCopy[idx1] = arrayCopy[idx2];
arrayCopy[idx2] = temp;
}
return arrayCopy;
}
function setGuessCount(newCount) {
guessCount = newCount;
document.getElementById("guesses-remaining").innerText = "Guesses remaining: " + guessCount + ".";
}
function updateGame(e) {
if (e.target.tagName === "LI" && !e.target.classList.contains("disabled")) {
// grab guessed word, check it against password, update view
var guess = e.target.innerText;
var similarityScore = compareWords(guess, password);
e.target.classList.add("disabled");
e.target.innerText = e.target.innerText + " --> Matching Letters: " + similarityScore;
setGuessCount(guessCount - 1);
// check whether the game is over
if (similarityScore === password.length) {
toggleClasses(document.getElementById("winner"), 'hide', 'show');
this.removeEventListener('click', updateGame);
} else if (guessCount === 0) {
toggleClasses(document.getElementById("loser"), 'hide', 'show');
this.removeEventListener('click', updateGame);
}
}
}
function compareWords(word1, word2) {
if (word1.length !== word2.length) throw "Words must have the same length";
var count = 0;
for (var i = 0; i < word1.length; i++) {
if (word1[i] === word2[i]) count++;
}
return count;
}
});