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

99
Visualizações
Validation of user input to see if it matches member of string array won't validate when they match only recognizes them as different

I don't understand why in the if statement it will execute the code for the input being different and say incorrect even when they are the same. I tried checking for the console.log in the console and it's not giving any errors. I know this isn't the case, but it's like it is not recognizing or reading the code. Sorry for long explanation I'm a beginner and its hard to explain what I'm trying to say.

Here is the code:

let output = document.querySelector("#typedWord");
let ansOutput = document.querySelector('[userOut]');
const display = document.querySelector("#wordDisplay");
const wordList = ['Carvedilol', 'Histrelin', 'Kadcyla', 'Mavyret', 'Paracetamol', 'Raloxifene', 'Saxagliptin'];
const random = Math.floor(Math.random() * wordList.length);

window.addEventListener('keydown', focusIn);

function focusIn(event) {
  const wordList = ['Carvedilol', 'Histrelin', 'Kadcyla', 'Mavyret', 'Paracetamol', 'Raloxifene', 'Saxagliptin'];
  const random = Math.floor(Math.random() * wordList.length);
  const ranWord = wordList[random];
  const display = document.querySelector("#wordDisplay");
  let output = document.querySelector("#typedWord");
  if (event.keyCode === 32) {
    display.innerHTML = ranWord;
    setTimeout(time, 6000);
  }
  if (output.addEventListener('keydown', handleKeyUp));

  function time(event) {
    const timeDisplay = document.querySelector("#timeDisplay");
    timeDisplay.innerHTML = "Time Up!";
    console.log("time event")
  }

  function handleKeyUp(event) {
    let ansOutput = document.querySelector('[userOut]')
    if (event.keyCode === 13 && ansOutput === ranWord) {
      event.preventDefault();
      event.target.blur();
      timeDisplay.innerHTML = "Correct!";
      console.log('executed')
    }

    if (event.keyCode === 13 && ansOutput !== ranWord) {
      event.preventDefault();
      event.target.blur();
      timeDisplay.innerHTML = "Incorrect!";
      console.log('executed')
    }
  }
}
<header>
  <h1>Type A Word Challenge!</h1>
</header>
<main>
  <div id="wordDisplay"></div>
  <br>
  <div id="timeDisplay"></div>
  <br>
  <label>Type Word Here</label>
  <input type="text" id="typedWord" name="userOut">
  <h3>Press Spacebar to Begin</h3>
</main>

almost 4 years ago · Santiago Trujillo
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