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.