Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda