Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
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 Respostas
Responde à pergunta

0

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

Math.floor
almost 4 years ago · Santiago Trujillo Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda