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

218
Vistas
add Array using for loop (JS)

I'm recreating a hangman game where I create a random word and conceal this with "". i.e if the word is "monkey" then the array should be ["", "", "", "", "", "_"].

With the following code, the alert ends up displaying ",,,,," or "". I've tried searching for the answer, tried .push method and rearranged the code to no avai.

also, this code is example code out of a beginners book so I'm not sure if it's simply just outdated now.

Appreciate if anyone can shed some light on how I could get this to work with basic beginner syntax.

// Attempt 1:

var words = [
    "javascript",
    "monkey",
    "amazing",
    "pancake"
];

var word = words[Math.floor(Math.random() * words.length)];
console.log(word);

var answerArray = [];
for (var i = 0; i < word.length; i++); {
    answerArray[i] = "_";
}

alert(answerArray);
// outputs ",,,,,,_"

///Attempt 2:
//Same code as above but with:

alert(answerArray.join(" "));
// outputs single "_"

///Attempt 3
//as above but with:

var answerArray = [];
for (var i = 0; i < word.length; i++); {
    answerArray.push("_");
}

alert(answerArray.join(" "));
// outputs single "_"
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

First Quick tipp use let instead of var. Then in your first attempt in the initialization of the for loop, you have a semicolon after the closing bracket. This terminates the statement and the for-body (between the {}) gets not executed. so remove that semicolon and you're good to go

about 4 years ago · Juan Pablo Isaza Denunciar

0

      const words = [
          "javascript",
          "monkey",
          "amazing",
          "pancake"
       ];

      var word = words[Math.floor(Math.random() * words.length)];
      console.log(word);

      var answerArray = [];
      for (var i = 0; i < word.length; i++) {
              answerArray[i] = ",";
      }

      console.log(answerArray[0])

      alert(answerArray);

Remove the semicolon in for loop and you are ready to go.

      [ ',', ',', ',', ',', ',', ',', ',', ',', ',', ',' ]
about 4 years ago · Juan Pablo Isaza Denunciar

0

Remove semicolon after the for loop declaration and before the body of for loop. This semicolon is making your for loop to end as soon as it get declared.

var words = [
"javascript",
"monkey",
"amazing",
"pancake"
];

var word = words[Math.floor(Math.random() * words.length)];
console.log(word);

var answerArray = [];
for (var i = 0; i < word.length; i++) {
answerArray[i] = "_";
}

alert(answerArray);
about 4 years ago · Juan Pablo Isaza Denunciar
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