1var secretWord = 'apple',
2 letters = '';
3
4function play(letter) {
5 if(letters.indexOf(letter) != -1) {
6 console.log("You've already tried '" + letter + "' ...");
7 }
8 else {
9 letters += letter;
10
11 if(secretWord.indexOf(letter) == -1) {
12 console.log(letter + ' -> wrong!');
13 }
14 else {
15 var progress = secretWord.replace(new RegExp('[^' + letters + ']', 'g'), '_');
16
17 console.log(letter + ' -> ' + progress);
18
19 if(progress == secretWord) {
20 console.log('Well done!');
21 }
22 }
23 }
24}
25
26play('i');
27play('e');
28play('p');
29play('e');
30play('a');
31play('l');