-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest-js-2.js
124 lines (100 loc) · 3.01 KB
/
Test-js-2.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
124
$(document).ready(function() {
var playersGuess,
var winningNumber;
var guesses = []
var direction;
var withinBounds = 0;
var hintsLeft;
var guessesLeft;
playAgain();
/* **** Guessing Game Functions **** */
// Generate the Winning Number
function randInt(a, b) {
if (b > a) {return Math.floor((Math.random() * (b + 1 - a) + a);}
else {return Math.floor((Math.random() * (a + 1 - b) + b);}
}
function generateWinningNumber(){
winningNumber = randInt(1, 100);
}
// Fetch the Players Guess
function playersGuessSubmission(){
playersGuess = $('#guess').val().parseInt();
$('#guess').val("");
}
// Determine if the next guess should be a lower or higher number
function lowerOrHigher(){
if(playersGuess > winningNumber) {
direction = "higher";
withinBounds = randInt(playerGuess, 100);
}
if(playersGuess < winningNumber) {
direction = "lower";
withinBounds = randInt(1, playerGuess);
}
}
function guessMessage() {
var str = "Your number is " + direction + " and within " + withinBounds + " from me. Keep trying."
}
// Check if the Player's Guess is the winning number
function checkGuess(){
if(!isInArray(playersGuess, guesses)) {
guesses.push(playersGuess);
guessesLeft--;
}
else {
$('.hey-girl p').text("This is an old guess. Try a different number.");
}
if (playersGuess === winningNumber) {
$('.hey-girl p').text("That's correct! We are so in sync.");
}
else {
$('.gh-count guesses-left').text(guessesLeft.toString());
if(guessesLeft === 0) {
$('.hey-girl p').text("I win this time. I was thinking of " + winnningNumber + ". Are you my reward?");
}
else {
lowerOrHigher();
$('.hey-girl p').text(guessMessage());
}
}
}
function isInArray(num, arr) {
var i = 0;
while (i < arr.length) {
if (arr[i] === num) {
return true;
}
i++;
}
return false;
}
// Create a provide hint button that provides additional clues to the "Player"
function provideHint(){
hintsLeft--;
$('.gh-count hints-left').text(hintsLeft.toString());
var arr = [];
arr.push(winningNumber);
var rand;
while(arr.length <= guessesLeft + 1) {
rand = randInt(withinBounds, winningNumber);
if(!isInArray(rand, arr)) {
arr.push(rand);
}
}
shuffle(arr);
$('.hey-girl p').text("One of these is the winning number: " + arr.toString());
}
// Allow the "Player" to Play Again
function playAgain(){
generateWinningNumber();
guessesLeft = 5,
hintsLeft = 5;
$('.gh-count guesses-left').text(guessesLeft.toString());
$('.gh-count hints-left').text(hintsLeft.toString());
$('.hey-girl p').text("I'm ready to play when you are.");
}
/* **** Event Listeners/Handlers **** */
$('.guess-button').on('click', function() {alert('works');});
$('.hint-button').on('click', provideHint);
$('.reset').on('click', playAgain);
}