I created a "mini-game". It is simply a reaction tester. All you do is try to click on the squares and circles as fast as you can.
What I want to do now is to have a play button. When a user clicks on the play button, it would give prompt the user or input the user's name and store it. Once the user starts playing, I want to store the highest score the (least time in this case) user gets, and display it in a table so it is publicly visible like a leader board.
I have asked a lot of my friends and people at my local meetup groups, and they have recommended me to use local storage or JSON. I have no experience with JSON at the moment. So how would I go about creating such a thing.
And I would love any criticisms or suggestions or comments or just anything to improve my code below.
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 15)];
}
return color;
}
var clickedTime;
var createdTime;
var reactionTime;
function makeBox() {
createdTime = Date.now();
var time = Math.random();
time = time * 3000;
setTimeout(function() {
if (Math.random() > 0.5) {
document.getElementById('box').style.borderRadius = "40px";
} else {
document.getElementById('box').style.borderRadius = "0px";
}
var top = Math.random();
top = top * 300;
var left = Math.random();
left = left * 500;
document.getElementById('box').style.top = top + "px";
document.getElementById('box').style.left = left + "px";
document.getElementById('box').style.backgroundColor = getRandomColor();
document.getElementById('box').style.display = "block";
createdTime = Date.now();
}, time);
}
document.getElementById('box').onclick = function() {
clickedTime = Date.now();
reactionTime = (clickedTime - createdTime) / 1000;
document.getElementById('time').innerHTML = reactionTime;
this.style.display = "none";
makeBox();
}
makeBox();
body {
font-family: Verdana, Geneva, sans-serif;
}
.container {
padding: 20px 20px;
text-align: center;
color: #204056;
}
#border {
border: 1px solid black;
border-color: #204056;
background-color: #F1F2F2;
width: 700px;
height: 700px;
position: fixed;
margin-left: 30%;
}
#box {
width: 80px;
height: 80px;
background-color: red;
display: none;
position: relative;
}
.bold {
font-weight: bold;
color: #46C9B6;
}
<div id="border">
<div class="container">
<h1>Test Your Reactions!</h1>
<p>Click on the boxes and circles as quickly as you can.</p>
<p class="bold">Your time: <span id="time">0</span>s</p>
<div id="box">
</div>
</div>
</div>
localStorage.setItem('name','Dave');&&localStorage.getItem('name');