-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22-2.js
More file actions
99 lines (82 loc) · 1.92 KB
/
22-2.js
File metadata and controls
99 lines (82 loc) · 1.92 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
window.onload = newgame;
window.onpopstate = popState;
var state, ui;
function newgame(playagain){
ui = {
heading: null,
prompt: null,
input: null,
low: null,
mid: null,
high: null
};
//
for(var id in ui){
ui[id] = document.getElementById(id);
}
//
ui.input.onchange = handleGuess;
//
state = {
n: Math.floor(99 * Math.random()) + 1,
low: 0,
high: 100,
guessnum: 0,
guess: undefined
};
//
display(state);
}
function save(sate){
if (!history.pushState){
return;
}
}
function popState(event){
if (event.state){
state = event.state;
display(state);
} else {
history.replaceState(state, "", "#guess" + state.guessnum);
}
}
function handleGuess(){
var g = parseInt(this.value);
if ((g > state.low) && (g < state.high)){
if (g < state.n) {
state.low = g;
} else if (g > state.n) {
state.higt = g;
}
state.guess = g;
state.guessnum++;
save(state);
display(state);
} else {
alert("Please enter a number greater than " + state.low +
" and less than " + state.high);
}
}
function display(state){
ui.heading.innerHTML = document.title =
"I'm thinking of a number between " +
state.low + " and " + state.high + ".";
ui.low.style.width = state.low + "%";
ui.low.style.width = (state.high-state.low) + "%";
ui.high.style.width = (100 - state.high) + "%";
ui.input.style.visibility = "visible";
ui.input.value = "";
ui.input.focus();
//
if (state.guess === undefined){
ui.prompt.innerHTML = "Type your guess and hit Enter:";
} else if(state.guess < state.n){
ui.prompt.innerHTML = state.guess + " is too low. Guess again: ";
} else if(state.guess > state.n){
ui.prompt.innerHTML = state.guess + " is too high. Guess again: ";
} else {
ui.input.style.visibility = "hidden";
ui.heading.innerHTML = document.title = state.guess + " is correct!";
ui.prompt.innerHTML = "You Win! <button onclick='newgame(true)'>Play Again</button>";
}
}