After generating the number of dashes according to the length of the word, how would I go about replacing these dashes with the correct letter the user inputs. In most examples, I have seen people using arrays for this purpose. But since my dashes are hr elements, how should I be doing this?
The following is my javascript code:
var words = ['quaffle', 'bludger', 'pensieve', 'harry', 'lupin',
'butterbeer', 'polyjuice', 'patronus', 'horcrux', 'voldemort'
];
correctletters = '';
missedletters = '';
function getRandomWord() {
randomWord = words[Math.floor(Math.random() * words.length)];
wordlength = randomWord.length;
}
function drawDashes() {
for (var i = 0; i < wordlength; i++) {
var dash = document.createElement("HR")
document.body.appendChild(dash);
}
}
function getGuess() {
letterGuessed = document.getElementById("inputfield").value.toLowerCase();
document.getElementById("inputfield").value = null;
alert(randomWord);
alert(letterGuessed);
r = randomWord.includes(letterGuessed);
if (r == true) {
correctletters = correctletters + letterGuessed;
alert("correct letters: " + correctletters);
} else {
missedletters = missedletters + letterGuessed;
alert("missed letters: " + missedletters);
}
guess = missedletters + correctletters;
displayGuessedLetters();
}
function displayGuessedLetters() {
guessedletters = document.createElement("div");
content = document.createTextNode(guess);
guessedletters.appendChild(content);
container = document.getElementById("inputcontainer");
container.appendChild(guessedletters);
guessedletters.style.cssText = 'font-family: "Josefin Sans"; font-size:
30 px;
text - transform: uppercase;
color: rgb(255, 85, 49);
';
}
function replaceDashes() {
/*Code to replace dashes here.*/
}