I'm trying to make a quiz game for a class and I can't seem to find the problem.
function askQuestions(random) {
var random = math.floor(math.random() * testQuestions.length);
var start = document.getElementById(testStart);
start.addEventListner('click', askQuestions);
if (start) {
document.write(random);
}
return;
}
This is the function and I have an array of objects, which is referenced in the "random" variable. I'm totally lost and I know I'm missing something simple. The purpose of this function is to display a random question from the array testQuestions.
Math is a global object and it starts with a capital M
Math.floor
You are adding the event listener inside the function. The function will only run if you call it, so the event listener will never be added. Additionally, document.write() will overwrite the entire page. I changed it to use document.createElement() and document.body.appendChild(). Change your code to this:
function askQuestions(random) {
var random = Math.floor(Math.random() * testQuestions.length);
var el = document.createElement('p');
el.textContent = random.toString();
document.body.appendChild(el);
}
var start = document.getElementById(testStart);
start.addEventListner('click', askQuestions);