-
Notifications
You must be signed in to change notification settings - Fork 0
/
pendu.js
119 lines (102 loc) · 2.81 KB
/
pendu.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
var Pendu = function () {
var that = this;
var counter = document.getElementById('count');
var input = document.getElementById('lol');
var letters_holder = document.getElementById('letters');
this.letters;
var counter_start = 11;
var words = ['peste', 'imitateur', 'empreinte', 'quadrupède', 'panier'];
var guesses = [];
this.word;
var word_length;
this.getRandomArbitrary = function(min, max) {
return Math.floor(Math.random() * (max - min) + min);
};
var place_the_letter = function (letter) {
var validated = false;
for (var i = 0; i < word_length; i++) {
if (that.word[i] === letter) {
that.letters[i].innerHTML = letter;
validated = true;
}
}
return validated;
};
var check_the_input = function() {
return input.value;
};
var validate_letter = function (text) {
if (text.length === 1) {
if (guesses.indexOf(text) === -1) {
guesses.push(text);
return true
} else {
return 'already dood';
}
}
return false;
};
var is_finished = function () {
for (var i = 0; i < that.letters.length; i++) {
if (that.letters[i].innerHTML === '_') {
return;
}
}
alert('BRAVOOOOO !!!!');
window.location = 'https://youtu.be/oTyNVLlFiys?t=10s';
};
var reset_value = function() {
input.value = '';
};
var trigger = function(keydown) {
if (keydown.keyCode === 13) {
var letter = check_the_input();
var is_good = validate_letter(letter)
if (true === is_good) {
if (place_the_letter(letter)) {
is_finished();
} else {
alert('This letter is not in my word :/');
display_chances();
}
} else if ('already dood' === is_good) {
alert('You already picked this letter...');
} else {
alert('YOU MUST CHOOSE ONE LETTER BRUH !!!');
}
reset_value();
}
};
var enable_input = function () {
window.addEventListener('keydown', trigger)
};
var display_chances = function () {
if (--counter_start > 0) {
counter.innerHTML = counter_start;
} else {
window.location = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
}
};
var display_underscores = function() {
for (var i = 0; i < word_length; i++) {
var span = document.createElement('span');
span.classList.add('blank');
span.innerHTML = "_";
letters_holder.appendChild(span);
}
that.letters = document.getElementsByClassName('blank');
};
var initialize_pendu = function () {
that.word = words[that.getRandomArbitrary(0, words.length)];
word_length = that.word.length;
display_chances();
enable_input();
display_underscores();
};
return {
init : function () {
initialize_pendu();
}
}
};
window.pendu = new Pendu();