My program has a button that moves when you click it and adds one to your score however the score doesn’t display correctly. Here is my HTML:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p id="demo">Score: 0</p>
<button id="move-button" onclick="moveButton()"></button>
<script src="js.js"></script>
</body>
</html>
And here is the JavaScript:
let score = 0;
function moveButton()
{
score= score + 1
document.getElementById("demo").innerText = "Score:", score;
const btn = document.getElementById('move-button');
const maxX = window.innerWidth - btn.offsetWidth;
const maxY = window.innerHeight - btn.offsetHeight;
const randomX = Math.floor(Math.random() * maxX);
const randomY = Math.floor(Math.random() * maxY);
btn.style.left = `${randomX}px`
btn.style.top = `${randomY}px`
}
This is the css code in case it helps:
body{
display: flex;
overflow: hidden;
}
button{
position: absolute;
align-self: center;
height: 15px;
}
What can i do to make the score display correctly?
let score = 0;
function moveButton()
{
score= score + 1
document.getElementById("demo").innerText = "Score:", score;
const btn = document.getElementById('move-button');
const maxX = window.innerWidth - btn.offsetWidth;
const maxY = window.innerHeight - btn.offsetHeight;
const randomX = Math.floor(Math.random() * maxX);
const randomY = Math.floor(Math.random() * maxY);
btn.style.left = `${randomX}px`
btn.style.top = `${randomY}px`
}
body{
display: flex;
overflow: hidden;
}
button{
position: absolute;
align-self: center;
height: 15px;
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Document</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p id="demo">Score: 0</p>
<button id="move-button" onclick="moveButton()"></button>
<script src="js.js"></script>
</body>
</html>