-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
104 lines (83 loc) · 1.86 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
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numbers = "1234567890";
var letters = alpha + numbers;
function chooseRandomLetter() {
return letters.charAt(Math.floor(Math.random() * letters.length));
}
var score = 0;
var activeLetter = "A";
function displayNewLetter() {
var newLetter = chooseRandomLetter();
activeLetter = newLetter;
var newLetterDisplay = $("<div/>", {
text: newLetter,
class: "new-letter-display"
});
var container = $(".container").html(newLetterDisplay);
}
function init() {
hideTryAgain();
hideFireworks();
displayNewLetter();
updateScore();
}
function convertCharCodeToLetter(charCode) {
// charCode needs to equal
var letter = String.fromCharCode(charCode);
var upperLetter = letter.toUpperCase();
var lowerLetter = letter.toLowerCase();
var upperCode = upperLetter.charCodeAt(0);
var lowerCode = lowerLetter.charCodeAt(0);
return {
letter: letter,
upperLetter: upperLetter,
lowerLetter: lowerLetter,
upperCode: upperCode,
lowerCode: lowerCode
}
}
function incrementScore() {
score += 1;
updateScore();
}
function updateScore() {
$('.score').text(score);
}
function hideFireworks() {
$('.pyro').hide();
}
function showFireworks() {
$('.pyro').show();
window.setTimeout(function(){
hideFireworks();
}, 1000);
}
function immediateHideTryAgain() {
$('.try-again').hide();
}
function congratulate() {
showFireworks();
incrementScore();
displayNewLetter();
immediateHideTryAgain();
}
function hideTryAgain() {
$('.try-again').fadeOut();
}
function tryAgain() {
$('.try-again').show();
window.setTimeout(function(){
hideTryAgain();
}, 2000);
}
$(document).keypress(function(e){
var letterObj = convertCharCodeToLetter(e.charCode);
if (activeLetter == letterObj.lowerLetter || activeLetter == letterObj.upperLetter) {
congratulate();
} else {
tryAgain();
}
});
$(document).ready(function() {
init();
});