im trying to make a test for class and i cant 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. im totally lost and i know im missing something simple. this functions purpose is to display a random question from the array "testQuestions".
CodePudding user response:
Math is a global object and it starts with a capital M
Math.floor
CodePudding user response:
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);