-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (85 loc) · 3.41 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
let computerGuess;
let userGuesses = [];
let attempts = 0;
let maxGuess;
let low = 1;
let high = 100;
function updateGuess() {
const lowValue =document.getElementById('low');
lowValue.style.flex = low + '%';
lowValue.style.background = '#ef7b54';
console.log('low' + low);
const space =document.getElementById('space');
space.style.flex = high - low + '%';
space.style.background = '#83e1d0';
console.log('high' + high);
const highValue =document.getElementById('high');
highValue.style.flex = 100 - high + '%';
highValue.style.background = '#ef7b54';
rangeOutput()
}
function rangeOutput() {
const range = document.getElementById('rangeOutput');
range.innerText = `${low} - ${high}`;
range.style.marginLeft = low + '%';
range.style.marginRight = 100 - high + '%';
range.classList.add("flash");
}
function init() {
computerGuess = Math.floor(Math.random() * 100) + 1 ;
document.getElementById('newGameButton').style.display = 'none';
document.getElementById('gameArea').style.display = 'none';
console.log(computerGuess)
}
function easy() {
document.getElementById('welcomeScreen').style.display = 'none';
document.querySelector('#gameArea').style.display = 'block';
maxGuess = 10;
}
function hard() {
document.getElementById('welcomeScreen').style.display = 'none';
document.querySelector('#gameArea').style.display = 'block';
maxGuess = 5;
}
function gameEnded() {
document.getElementById('newGameButton').style.display = 'inline';
document.getElementById('inputBox').setAttribute("readonly", "readonly");
gameEnded()
}
function newGame() {
window.location.reload();
}
function compareGuess() {
const userGuess = Number(document.querySelector('#inputBox').value);
userGuesses.push(" " + userGuess);
document.getElementById('guesses').innerHTML = userGuesses;
attempts ++;
document.getElementById('attempts').innerHTML = attempts;
console.log(maxGuess);
if (attempts < maxGuess) {
if (computerGuess > userGuess) {
if (userGuess > low) low = userGuess;
document.getElementById('textOutput').innerHTML = "Your Guess IS Too Low";
document.getElementById('inputBox').value = "";
} else if ( computerGuess < userGuess) {
if (userGuess < high) high = userGuess;
document.getElementById('textOutput').innerHTML = "Your Guess IS Too High";
document.getElementById('inputBox').value = "";
} else { document.getElementById('textOutput').innerHTML = "Correct you got it in " + attempts + ' attempts';
document.getElementById('inputBox').value = "";
gameEnded()
}
} else {
if (computerGuess > userGuess) {
if (userGuess > low) low = userGuess;
document.getElementById('textOutput').innerHTML = "You loss the correct number is " + computerGuess;
} else if ( computerGuess < userGuess) {
if (userGuess < high) high = userGuess;
document.getElementById('textOutput').innerHTML = "You loss the correct number is " + computerGuess;
} else { document.getElementById('textOutput').innerHTML = "Correct you got it in " + attempts + ' attempts';
gameEnded()
}
gameEnded()
}
updateGuess();
}