Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

219
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!