File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ const input = require ( 'sync-input' ) ;
2
+
3
+ const attempts = 8 ;
4
+ const title = `H A N G M A N` ;
5
+ console . log ( title ) ;
6
+ console . log ( ) ;
7
+
8
+ const words = [ "python" , "java" , "swift" , "javascript" ] ;
9
+ const word = words [ Math . floor ( Math . random ( ) * words . length ) ] ;
10
+
11
+ const hint = word . substring ( 0 , 3 ) + "-" . repeat ( word . length - 3 ) ;
12
+
13
+ let revealedWord = "-" . repeat ( word . length ) ;
14
+
15
+ for ( let i = 0 ; i < attempts ; i ++ ) {
16
+ console . log ( revealedWord ) ;
17
+ const letter = input ( "Input a letter: " ) ;
18
+
19
+ if ( word . includes ( letter ) ) {
20
+ for ( let j = 0 ; j < word . length ; j ++ ) {
21
+ if ( word [ j ] === letter ) {
22
+ revealedWord = revealedWord . substring ( 0 , j ) + letter + revealedWord . substring ( j + 1 ) ;
23
+ }
24
+ }
25
+ } else {
26
+ console . log ( "That letter doesn't appear in the word" ) ;
27
+ }
28
+ }
29
+
30
+ console . log ( "Thanks for playing!" ) ;
You can’t perform that action at this time.
0 commit comments