Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's very useful to think of the problem in terms of strings (or arrays of characters), after all that's what you're dealing with. Then when you get a working prototype you can mix it with the DOM and its methods, otherwise it all becomes a mess quickly.</p> <p>The steps and implementation of the game of hangman could be:</p> <ol> <li>Guess a letter. if letter exists replace placeholder with letter or else do nothing.</li> <li>Be able to guess more letters until all letters have been revealed.</li> <li>Inform the user of the progress so far.</li> </ol> <p>An implementation of this idea:</p> <pre><code>var hangman = function(word) { // String revealed so far, as an array var result = word.replace(/./g, '_').split(''); return { guess: function(letter) { // If word contains letter then replace // placeholder with letter for (var i=0; i&lt;word.length; i++) if (word[i].toLowerCase() == letter) result[i] = letter; return this; // chain }, get: function(f) { f = f || function(){}; var done = result.indexOf('_') == -1; return f.call(this, result.join(''), done); } }; }; </code></pre> <p>And you use it like:</p> <pre><code>var soFar = function(result, done) { // Interact with DOM here if (done) { console.log('Congrats! The word is :'+ result); return result; } console.log('Word so far: '+ result); return this; }; // Value from the DOM var word = document.getElementById('#word').value; var game = hangman(word) // assume word = "hello" .guess('l') .guess('o') .get(soFar); //^ Word so far: __ell console.log(game); //=&gt; {guess: [Function], get: [Function]} // Play more game = game .guess('e') .guess('h') .get(soFar); //^ Congrats! The word is: hello console.log(game); //=&gt; hello </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload