-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (89 loc) · 2.78 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
$(setup);
var letters,
lettersGuessed = [],
word,
wrongChoice = -1;
function setup() {
letters = $('.letters');
setupEvents();
}
function setupEvents() {
$('.letters').on('click', chooseLetter);
$('.btn').on('click', chooseWord)
}
function chooseWord() {
word = $('input').val();
$('#chosen-word').html(word);
$('.user-input').hide("slow");
// Setup spaces
var wordArray = word.split("");
console.log(wordArray)
$.each(wordArray, function(element) {
$('.chosen-word').append("<li>_</li>");
document.getElementById("number-of-letters").innerHTML =
"There are " + word.length + " letters in this word";
document.getElementById("rules").innerHTML =
"Guess the letters and if you are wrong well..."
$('.myButton').on('click', function() {
$(this).hide(".myButton");
})
})
}
function chooseLetter() {
var letter = $(this).text().toLowerCase();
lettersGuessed.push(letter)
checkLetter(letter)
console.log(lettersGuessed);
if(wrongChoice > 3){
$("#rules").html("")
$("#number-of-letters").html("You just got beat by the Hoff!");
$('#play-again').html('<button class="play-again-btn">Play Again</button>')
}
$('button.play-again-btn').on('click', playAgain);
}
function checkLetter(letter) {
// check where in the correct word, the chosen letter is found
var index = word.indexOf(letter);
// create array to store multiple indexs
var correctIndex = []
if (index == -1) {
wrongChoice += 1
console.log("wrong")
$($('.row-image')[wrongChoice]).css("visibility", "hidden");
} else {
// loop through the correct word, and check for multiple occurences
while (index !== -1) {
// if we find a correct index, push it into our array
correctIndex.push(index);
console.log(correctIndex)
// MAGIC
index = word.indexOf(letter, index + 1);
// correct guess
// for each of our indexs that we found, loop through, and add to html
$(correctIndex).each(function(index, letterIndex){
if(winner(lettersGuessed, word.split(""))){
$("#rules").html("")
$("#number-of-letters").html("You won! Hoff owes you a beer!")
$('#play-again').html('<button class="play-again-btn">Play Again</button>')
}
$($('.chosen-word 4 li')[letterIndex]).html(letter);
})
}
}
}
function playAgain(){
location.reload()
}
function winner(lettersGuessed, word){
var count = 0
for (var i = 0; i < word.length; i++) {
if(lettersGuessed.indexOf(word[i])>-1){
count++
}
};
if (word.length === count) {
return true
} else {
return false
};
}