3

SITUATION

I started to implement the game "HANGMAN" using only JavaScript and HTML on client side machines. I have completed all the logical work. Now only the aesthetics are remaining. And I am stuck up because we can't write to strings in JS.

a = "abcd"; a[0] = "e"; -- This type of thing is not allowed.

What I have done

CODE

The code I have tried, This does not work:

See here

I now have the following three arrays:

a -- The array that contains the letters of the word(for gel this would be g,e,l)

gai -- This array consists of the letters that have been entered by the user and are there in the word.(like g or e)

ag -- This array consists of the letters that have been entered by the user regardless of whether or not they are there in the word to be guessed or not.

What I need to do

I need to generate the output that would show the letter in case it has been guessed by the user and is there in the word(letters in the array gai) and _ in place of letters that have not yet been entered by the user.

What I want

A function that will return the desired output, as explained in the following example.

Example

Let the word to be guessed be:

together

User enters e:

The function should return: _ _ _ e _ _ e _

User then enters d, my program will prompt him to say that this letter is not there in the word.

User enters r

Function should return: _ _ _ e _ _ e r

And so on....

I hope you get the idea!

In case you don't get it(owing to my bad explanation or otherwise!!)

You can play a game of hangman here: Hangman

Just watch the output at the bottom of the screen... Thats what I want this function to generate.

Kindly help me with this issue!

Community
  • 1
  • 1
IcyFlame
  • 5,059
  • 21
  • 50
  • 74
  • 5
    You have described what you have done, but no code has been provided? Re-creating it on jsfiddle.net would help, or you **must** provide all the information required for us to re-create the entire functionality required to look at the problem. This includes all HTML/CSS/JavaScript code. Link to it if you have a live page. – Samuel Liew Jul 19 '13 at 08:23
  • so, you want us to show you how to replace the characters in some word not on some list with underscores and stitch with spaces? – John Dvorak Jul 19 '13 at 08:25
  • 2
    **Of course you can write to an array** A=[10,20,30]; A[1]=15; console.log(A); yields [10,15,30] – Paul Jul 19 '13 at 08:26
  • 1
    @Paul you can't write to a string, however – John Dvorak Jul 19 '13 at 08:28
  • 2
    @JanDvorak You can indirectly, with `split` and `join`. – tckmn Jul 19 '13 at 08:29
  • 1
    or make a new one with replace – Paul Jul 19 '13 at 08:29
  • @Doorknob ... as OTK has shown. But is it an obvious approach? – John Dvorak Jul 19 '13 at 08:30
  • @Doorknob: That indirect method is what I am looking for here! – IcyFlame Jul 19 '13 at 08:30
  • @IcyFlame Then see [the answer that was provided to your question](http://stackoverflow.com/a/17741439/1223693). – tckmn Jul 19 '13 at 08:30

3 Answers3

4

Reading just the start of your question (because no other code is provided), there is a problem which could be solved using native javascript functions.

var strarr = "Hello world!".split(''); // this will give you an array of characters to be manipulated
strarr[6] = "X"; // so you can do this
console.dir(strarr.join('')); // test it!

As an idea (not mine, its from comments) one could even wrap this simple code as a missing toCharArray() function and use that.

Another thing is that this method is fast enough even for relatively large bulk of text. For a test I used lorem ipsum with 1000 words. On my rather old dev machine, manipulation executes in milliseconds.

For more information, see this discussion.

Community
  • 1
  • 1
OzrenTkalcecKrznaric
  • 5,535
  • 4
  • 34
  • 57
2

Also you can use this function to set char at specified index of string:

function setCharAt(str,index,chr) {
    if(index > str.length-1) return str;
    return str.substr(0,index) + chr + str.substr(index+1);
}
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
-1

You could use regex to mask letters in a word using String.replace.

var word = "together"
  , correct = "";

function guess(c) {
  if (word.indexOf(c) > -1) {
    correct += c;
  }
  console.log(word.replace(new RegExp("[^" + correct + "]", "g"), "_"));
}
thgaskell
  • 12,772
  • 5
  • 32
  • 38