1
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>
-->
3
  • I just see a bunch of text. That either means it's bedtime for me or your question isn't very clear. Commented Feb 4, 2014 at 22:59
  • I'm new to JavaScript and probably my question isn't clear enough. I can't figure out how to create a function that will swap _ signs in the right position, with a character input by user. I will add the HTML code now so that it will be easier to find out what I mean. Commented Feb 4, 2014 at 23:35
  • People on SO often provide a jsFiddle to make it easier for others to help them. Commented Feb 4, 2014 at 23:40

1 Answer 1

1

You'll need to keep track of the original phrase as well as the clues.

var phrase = 'A phrase to guess',
  clue = '_ _____ __ _____';

function updateClue (guess) {
  // split the clue into an array
  clue = clue.split('');

  // loop through the characters in the phrase
  for (var i = 0; i < phrase.length; i++) {

    // if the letter in the phrase matches the guess
    if (phrase.charAt(i).toLowerCase() === guess.toLowerCase()) {

      // replace the corresponding character in the clue
      clue[i] = phrase.charAt(i);
    }
  }

  // convert the clue array back into a string
  clue = clue.join('');
  return clue;
}

Here's silly (working) example.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much blurd. I didn't realise it was so simple! That's what I was looking for. Thanks again!
You'll need to do some validation and whatnot, but this is the basic idea. Feel free to accept this answer. ;)
OK blurd, I totally accept your answer, however cannot vote at the moment (need to have 15 points of reputation). I appreciate that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.