-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (99 loc) · 3.28 KB
/
index.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
"use strict";
var min = 0
var max = 11
var Game = function(name){
this.name = name
this.purse = 100
// this.bet = null;
// this.guess = null;
// this.answer = null;
}
Game.prototype = {
"newRound" : function(guess,bet){
if (bet > this.purse) {
return feedback.innerHTML = "<span style='color:red;'>You can't bet more than your bank... Try again</style>"
} else {
this.answer = Math.floor(Math.random() * (max - min)) + min;
this.bet = parseInt(bet);
this.guess = guess;
this.updateBank(-this.bet);
this.checkAnswer();
}
},
"checkAnswer" : function(){
(this.guess == this.answer || this.guess == (this.answer+1) || this.guess == (this.answer-1)) ? this.answerRight() : this.answerWrong();
},
"answerRight" : function(){
var check = "<span style='color:green;text-shadow:0 0 5px green;'>✓</span>"
if(this.guess == this.answer){
feedback.innerHTML = check + "Answer: " + this.answer + " Guess: " + this.guess + ". Perfect! Double up!"
this.updateBank(this.bet*2)
} else {
feedback.innerHTML = "Answer: " + this.answer + " Guess: " + this.guess + ". Not bad... here's your money back"
this.updateBank(this.bet)
};
this.nextRound();
},
"answerWrong" : function(){
feedback.innerHTML = "<span style='color:red;'>✗</span> Answer: " + this.answer + " Guess: " + this.guess + ". Sorry!"
this.nextRound();
},
"nextRound" : function(){
if (this.purse <= 0 || this.purse == "0") {
betForm.style.display = "none";
tryAgain.style.display = "inline-block";
feedback.innerHTML = "<span style='color:red;'>GAME OVER!</span> " + this.name + ". Refresh the page to try again"
} else {
return false;
};
},
"updateBank" : function(money){
this.purse += money
bank.innerHTML = "$" + this.purse
},
}
var feedback = document.querySelector('#feedback');
var bank = document.querySelector('#bank');
var nameForm = document.querySelector('#name');
var nameField = document.querySelector('#nameField');
var console = document.querySelector('#console');
var betForm = document.querySelector('#bet');
var betField = document.querySelector('#betField');
var numberField = document.querySelector('#numberField');
var tryAgain = document.querySelector('#tryAgain');
tryAgain.style.display = "none";
var game;
feedback.innerHTML = "What's your name gambler?"
numberField.onkeyup = function(e){
if(e.target.value > 10){
e.target.value = 10
}
if(e.target.value < 0){
e.target.value = 0
}
}
betField.onkeyup = function(e){
if(e.target.value < 0){
e.target.value = ""
}
}
nameForm.onsubmit = function(e){
e.preventDefault();
game = new Game(nameField.value);
nameForm.style.display = "none";
betForm.style.display = "block";
console.style.lineHeight = '70px';
feedback.innerHTML = game.name + ", lets go! Choose a number and place your bet!"
bank.innerHTML = "$" + game.purse;
}
betForm.onsubmit = function(e){
e.preventDefault();
game.newRound(parseInt(numberField.value),betField.value);
}
tryAgain.onclick = function(e){
game = new Game(game.name);
feedback.innerHTML = "OK " + game.name + ", let's go again!"
bank.innerHTML = "$ " + game.purse
tryAgain.style.display = "none";
betForm.style.display = "block";
}