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

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

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 Relatório

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 Relatório

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 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