Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
im trying to make a quiz for my bootcamp and i cant figure out what is wrong with the code

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.

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Math is a global object and it starts with a capital M

Math.floor
almost 4 years ago · Santiago Trujillo Report

0

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);
almost 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!