1

I am currently trying to figure out how to replace the dashes with a correctly chosen letter input by user. I have the dashes displayed like so.

       var chosenWord = randomWords[Math.floor(Math.random()*randomWords.length)];
       var dashes = "";

       for (var x = 0; x < chosenWord.length; x++)
       {
       dashes += " - ";
       }
       document.getElementById("word").innerHTML = dashes; 

Now I need to know a concept for replacing the dashes with a correctly guessed letter. Does the variable dashes become an array? for testing I tried replacing characters in dashes by using charAt by doing

       // for (x = 0; x < chosenWord.length; x++)
       // {
           //dashes[x] = "a";
           //dahes.charAt(x) = 'a';
        // }
        dashes.charAt(0) = "a";
        document.getElementById("test2").innerHTML = dashes;

But both the commented out code and one below it do not work. Another method I thought of but have not tried is to create an empty array and assign it the length of the randomly chosen string from another array, and then fill the empty array with the dashes first, then when the user presses a letter, it would compare and if there is a match I would go to the index of the empty array where the match occurred and replace dash with the letter. I am new to javascript and am wondering if there is a way to do this using very beginner javascript syntax.

henhen
  • 151
  • 1
  • 1
  • 12
  • `document.getElementById("test2").innerHTML = dashes.replaceAll('a', '-')` – Steeve Pitis Oct 07 '16 at 08:14
  • replaceAll seems good, but what if I need to replace just a single dash at a certain index in the string? – henhen Oct 07 '16 at 08:18
  • Unfortunately this is now closed, but I'd do it like this: https://jsfiddle.net/teb9vzqy. This creates two arrays, the `word` array which contains the real letters, and the `dashes` array which is initially filled with the same number of dashes as the `word` array has items. It then uses two while loops to prompt the user to make a guess, then check if the guess is correct; if the guess is correct, it nullifies the letter within the `word` array and replaces the dash within the `dashes` array at the correct position. It then converts the `dashes` array into a string representation using `join` – James Donnelly Oct 07 '16 at 08:21
  • In regards to my previous comment, the initial word in that JSFiddle demo is "foo". If you enter "f", then "o" it will complete the game (`result` will contain the word `foo`). If, however, you enter a load of non-matching characters, after 5 `attempts`, the game will also end and `result` will look like an incomplete word made up of dashes (i.e. if you entered "f" and then a load of gibberish, the `result` string would be `"f--"`. – James Donnelly Oct 07 '16 at 08:26
  • thank you James, I appreciate this. Very helpful – henhen Oct 07 '16 at 08:33
  • I see that in the jsfiddle example, he just fills each index in the array with a single character. I am trying to make one where each index is filled with an actual word – henhen Oct 07 '16 at 08:36

0 Answers0