func = function() {
val = document.getElementById('word').value;
first = val.charAt(0).toUpperCase();
rest = val.substring(1).toLowerCase();
valConvert = first + rest;
undersc = '_';
fullUndersc = '';
for (var i = 2; i < val.length; i++) {
fullUndersc = fullUndersc + undersc;
}
wordToGuess = valConvert.charAt(0) + fullUndersc + valConvert.slice(-1);
document.getElementById('form').innerHTML = wordToGuess;
document.getElementById('letterIn').style.display = 'block';
guess = function() {
var charac = document.getElementById('letter').value;
for (var i = 1; i < val.length -1; i++) {
if (charac == valConvert.charAt(i)) {
wordToGuess = wordToGuess.replace(new RegExp(wordToGuess.charAt(i), 'i'), charac);
document.getElementById('form').innerHTML = wordToGuess;
}
}
}
}
The outer function takes user's input, converts letters to a proper case (upper case and lower case), leaves first and last letter as it is and swaps the rest of characters with _.
The inner function (guess) again takes user's input (just one character) and checks whether or not the character exists in user's previous input. This function works but I want it to replace _ with the input letter IN THE PROPER POSITION.
In short - I am trying to make The Hangman game.
Any help will be much appreciated.
<!--HTML
<form onSubmit="func(); return false" id="form">
Type in a word below<br /><input class="" id="word" type="text" autofocus />
</form>
<form onSubmit="guess(); return false" id="letterIn">
Enter a letter: <input class="" id="letter" type="text" size="1" maxlength="1" autofocus />
</form>
-->