The idea is that the progress bar should increase when you click the button, for example, if you click on the button
play
the happiness
should increase.
JavaScript
x
17
17
1
const cleanBtn = document.querySelector(".clean");
2
const feadBtn = document.querySelector(".fead");
3
const playBtn = document.querySelector(".play");
4
5
6
7
cleanBtn.addEventListener("click", () => {
8
let health = document.getElementById("myHappines");
9
health.style.width = 0;
10
if (health.style.width > 100 % ) {
11
health.style.width = 0 % ;
12
else {
13
health.style.width += 10 % ;
14
}
15
}
16
})
17
JavaScript
1
67
67
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7
<title>Virtual Pet</title>
8
<!--font-awesome-->
9
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
10
<!--styles-->
11
<link rel="stylesheet" href="styles.css">
12
</head>
13
<body>
14
15
<div class="sections-container">
16
<div class="section1">
17
<div class="pet-info">
18
<ul>
19
<div id="basic">
20
<ul>Name: Elis</ul>
21
<ul>Age: 2 months</ul>
22
</div>
23
<div class="health">Health
24
<div id="myProgressHealth">
25
<div id="myHealth"></div>
26
</div>
27
28
</div>
29
<div class="hunger">Hunger
30
<div id="myProgressHunger">
31
<div id="myHunger"></div>
32
</div>
33
</div>
34
35
<div class="happiness">Happiness
36
<div id="myProgressHappiness">
37
<div id="myHappiness"></div>
38
</div>
39
</div>
40
</ul>
41
</div>
42
<button class="btn-settings">Settings</button>
43
</div>
44
<div class="section2">
45
<div class="pet-interactions">
46
<ul>
47
<button class="clean">Clean</button>
48
<button class="fead">Fead</button>
49
<button class="play">Play</button>
50
</ul>
51
</div>
52
<div class="game-control">
53
<li id="name-input">Pet Name <input type="text"></input></li>
54
<li>Game reset button <button class="btn">Reset</button></li>
55
</div>
56
<div class="end-game-message">
57
<p>Goodbye</p>
58
</div>
59
</div>
60
61
</div>
62
63
<!--javascript-->
64
<script src="app.js"></script>
65
</body>
66
</html>
67
Advertisement
Answer
There are many issues and typos in your code. Clean programming will solve them all.
- You want a bar for which HTML has a specific tag for:
<progress>
. Use it instead ofdiv
s! - Use a
ternary conditional operator
to check if the value is 100 or below and then reset the health bar or add 10% to it:
JavaScript
1
6
1
const cleanBtn = document.querySelector('.clean');
2
3
cleanBtn.addEventListener('click', () => {
4
let health = document.getElementById('myHappiness');
5
health.value = (health.value === health.max) ? 0 : health.value + 10;
6
})
JavaScript
1
5
1
<button class="clean">Clean</button>
2
3
<br>
4
5
<label for"myHappyness">Happiness:</label><progress id="myHappiness" value ="0" max="100"></progress>
The main issue with your code where the many typos. Among that was that you used < 100 %
which will divide 100 by an undefined amount and look for the remainder. %
is used as a calculation for a remainder (100 % 3
= 1
) . To use 100%
in the way you intended to, you have to use it as a string.