www.webdeveloper.com
Results 1 to 2 of 2

Thread: How to add characters of the word in specific order?

  1. #1
    Join Date
    Nov 2012
    Posts
    1

    How to add characters of the word in specific order?

    I am totally new in javascript and trying to make hangman game, I can take letters from users and check it perfectly .. but i have faced a problem in putting the letters (the right letters which the user choosing) in the right order like:
    - i have a word table
    - the user clicked on h and h is in the word chair
    - the user clicked on r and r is in the word chair
    - the user clicked on a and a is in the word chair
    - the user clicked on i and i is in the word chair
    - the user clicked on c and c is in the word chair
    now the word has been written hraic

    the question is : what can i do to make the letters in right order.

    Thanks in advance

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    When you write out the word loop through the letters in chair and check if the letter has been clicked. If so then add the letter to the "final" text. Example:
    Code:
    var clickedLetters = 'hra';
    var correctWord = 'chair';
    var finalWord = '';
    for (var i = 0, len = correctWord.length; i < len; i++) {
    	var letter = correctWord[i];
    	if (clickedLetters.search(letter) != -1)
    		finalWord += letter;
    	else
    	​	finalWord += '_';
    }
    alert(finalWord);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles