Skip to main content

hangman.js

gist link


function hangman(word) {
    var reference = word.replace(/[a-zA-Z]/g,'-');
    function reveal(letter) {
        reference = reference.split('').map(function(v, i){
            return word[i] === letter ? letter : v;
        }).join('');
        return reference;
    };
    return {
        reveal: reveal
    };
}

var game = hangman('carrot');
console.log(game.reveal('r'));
console.log(game.reveal('a'));