I am currently creating a project in Javascript that is intended to be a sort of a "reaction game". After pressing a "begin" button, a shape will pop up on screen per the color, size, location (margins), and shape (either a box or a circle) decided by some helper methods. Goal is to click the shape asap and the page will then print the time taken. A new shape will then display and the cycle repeats.
However, once i press the "begin" button, nothing appears on screen. The shape is nowhere to be seen.
Can anyone please help make the shape appear? I think it has something to do with the style sheet or something, but i'm not too sure.
<html>
<head>
<title>Reaction Tester</title>
</head>
<body>
<h1>Test Your Reactions!</h1>
<p>Click on the boxes and circles as quickly as you can!</p>
<h2 id="recorded-time"></h2>
<button id="start-button">Begin!</button>
<div id="current-shape"></div>
<script type="text/javascript">
var beginTime = 0.0; //default value
document.getElementById("start-button").onclick = function() {
document.getElementById("start-button").style.display = "none";
newShape();
}
function decideShape() { //chooses between a circle or box(square)
var x = Math.random();
if(x < 0.5) {
return("circle");
} else {
return("box");
}
}
function decideColor() { //chooses between a list of 8 colors
var x = Math.random();
if(x < 0.125) {
return("red");
} else if(x < 0.25) {
return("blue");
} else if(x < 0.375) {
return("yellow");
} else if(x < 0.5) {
return("green");
} else if(x < 0.625) {
return("purple");
} else if(x < 0.75) {
return("black");
} else if(x < 0.875) {
return("gray");
} else {
return("#00FFFF"); //cyan
}
}
function decideSize() { //self explanatory
var value; //circle - radius, box - half of side length
value = Math.floor(Math.random() * 75) + 25; //diameter/side length set to be between 50 and 199
return value;
}
function decideMargin(size) { //depends on size of shape
var value;
value = Math.floor(Math.random()*(400 - size)) + size;
return value;
}
document.getElementById("current-shape").onclick = function() {
newShape();
}
function beginTimer() {
var bT = new Date();
beginTime = bT;
}
function stopTimer() {
var endTime = new Date();
var elapsedTime = endTime - beginTime;
document.getElementById("recorded-time").innerHTML = "Your time: " + (elapsedTime/1000.0) + " seconds";
}
function newShape() {
if(beginTime != 0.0) {
stopTimer();
}
var nextShape = decideShape();
var nextColor = decideColor();
var nextSize = decideSize();
var nextLeftMargin = decideMargin(nextSize);
var nextTopMargin = decideMargin(nextSize);
document.getElementById("current-shape").style.backgroundColor = nextColor;
document.getElementById("current-shape").style.marginLeft = nextLeftMargin;
document.getElementById("current-shape").style.marginTop = nextTopMargin;
if(nextShape = "circle") {
document.getElementById("current-shape").style.borderRadius = 50;
} else { //nextShape = "box"
document.getElementById("current-shape").style.borderRadius = 0;
}
beginTimer();
}
</script>
</body>
</html>