-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
120 lines (86 loc) · 2.9 KB
/
Copy pathscript2.js
File metadata and controls
120 lines (86 loc) · 2.9 KB
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
$(document).ready(function() {
/*------- Variables and Functions -------*/
var answer = Math.floor((Math.random()*100)+1);
console.log("answer: " + answer);
var feedback;
var answers = [];
var differenceHot = 100 - answer;
var differenceCold = answer - 1;
var offset;
var win=false;
var meterCalibration = 2;
var changeFeedback = function(comment) {
$('#feedback').fadeOut().empty();
$('#feedback').val(comment).fadeIn();
$('#userInput').val("");
}
var placeAnswerHot = function(userInput) {
var userOffsetHot = 100 - userInput;
offset = Math.round(((userOffsetHot/differenceHot) * 100)/2);
console.log(offset);
$('#guesses').append("<div class='guess wrong' id='" + answers.length + "'style='left:" + (offset - meterCalibration) + "%'>" + userInput + "</div>");
$("#" + answers.length).fadeOut(5000);
}
var placeAnswerCold = function(userInput) {
var userOffsetCold = userInput - 1;
offset = Math.round(((userOffsetCold/differenceCold) * 100)/2);
console.log(offset);
$('#guesses').append("<div class='guess wrong' id='" + answers.length + "'style='right:" + (offset - meterCalibration) + "%'>" + userInput + "</div>");
$("#" + answers.length).fadeOut(5000);
}
var checkAnswer = function() {
var userIpt = $('#userInput').val();
var userInput = parseInt(userIpt, 10);
if (win) {
changeFeedback("You already won - hit reset!");
}
else if(isNaN(userInput)) {
userInput = 'x';
answers.push(userInput);
changeFeedback("Nice try, but your answer must be an number");
}
else if (userInput < 1 || userInput > 100) {
answers.push(userInput);
changeFeedback("Nice try, but your answer must be between 1 and 100");
}
else if (userInput > answer) {
answers.push(userInput);
changeFeedback("Too hot");
placeAnswerHot(userInput);
}
else if (userInput < answer) {
answers.push(userInput);
changeFeedback("Too cold");
placeAnswerCold(userInput);
}
else if (userInput == answer) {
answers.push(userInput);
changeFeedback("Got it - nice job! " + answers.length + " attempts.");
$('#guesses').append("<div class='guess correct' id='" + answers.length + "'style='left:49%'>" + userInput + "</div>");
win = true;
}
$('#answers').html("Attempts: " + answers.length);
$('#userInput').focus();
}
var reset = function() {
answer = Math.floor((Math.random()*100)+1);
answers = [];
$('#answers').html("Attempts: " + answers.length);
$('#guesses').empty();
$('#userInput').val("");
$('#userInput').focus();
changeFeedback("Okay - new game");
win=false;
}
/*-------- Start program ----------*/
$('#userInput').focus(); /*direct the focus to the number input area */
$('#answers').html("Attempts: " + answers.length);
changeFeedback("Guess a number between 1 and 100...");
$('#submit').click(checkAnswer);
$('#userInput').keyup(function(e) {
if (e.keyCode == 13) {
checkAnswer();
}
});
$('#reset').click(reset);
});