So I'm trying to master JavaScript and to practice I'm trying to make a reaction time test. The idea is very simple: you click on a button and after a random period of time the background changes colour and then you have to click on a second button which calls on a function that calculates your reaction time. Here is my JavaScript code:
function start(){
console.log(start);
var rndnr = (Math.round(Math.floor(Math.random() * 5) + 1)) * 1000);
setTimeout(function timeout() {
document.bgColor="ffff00";
start = new Date();
}, rndnr);
}
function stop() {
var onclick = new Date();
var time = onclick-start;
var total;
var avg;
var count = 1;
document.getElementById("try 1").innerHTML = time;
total = total + time;
avg = total / count;
count = count + 1;
document.getElementById("avg1").innerHTML = avg;
}
and these are my buttons:
<button id="button" onclick="start()" >start</button>
<button onclick="stop()">stop</button>
When I try to execute the script I get an error in my console log which says this: ReferenceError: start is not defined
. Where have I gone wrong?