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

173
Views
JavaScript - Need help explaining why closure is not working

I'm trying to build a basic hangman game. I'm trying to get 'runcode' function to run once using closures. However I'm not sure why my closure is not working. I'm creating an object then using the map method to replace the letters with underscores.

const hangMan = () => {
  const word = "honduras";
  const arrayWord = word.split("");
  let dashedWord = [];

  const hashWord = () => {
    dashedWord = arrayWord.map((el, index) => {
      dashedWord.push(el);
      return (el = "_");
    });
    // console.log(`dashedword function = ${dashedWord}`);
    // return dashedWord;
  };

  // console.log(prevWord);
  // console.log(hashedWord);

  // console.log((arrayWord[0] = "t"));
  // console.log(arrayWord.indexOf("t"));
  // console.log(typeof arrayWord);
  // console.log(arrayWord);

  function findIndex(word, index) {
    return word.indexOf(index);
  }

  function updateFoundLetter(guess) {
    let executed = false;
    console.log("fired updateFoundLetter");

    const runcode = () => {
      console.log(`UpdateFoundLetter wordHashed = ${executed}`);
      if (!executed) {
        hashWord();
        executed = true;
        console.log("ran true");
      } else {
         let index = findIndex(word, guess);
        dashedWord[index] = guess;
      }

      //console.log("hashedword index " + dashedWord[index]);
      // console.log(index);
    };

    return runcode;
  }

  //console.log(`before calling updateFoundLetter ${wordHashed}`);
  let myFunc = updateFoundLetter("h", word);
  myFunc();

  console.log(dashedWord);

  //console.log(`after calling updateFoundLetter ${wordHashed}`);
  //console.log(findIndex(word, "o"));
};

hangMan();

about 4 years ago · Juan Pablo Isaza
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!