1+ import { gamelogic } from "../src/index.js" ;
12import readlineSync from 'readline-sync' ;
2- console . log ( "Welcome to the Brain Games!" ) ;
3- const name = readlineSync . question ( "May I have your name? " ) ;
4- console . log ( `Hello, ${ name } !` ) ;
5- console . log ( 'Answer "yes" if the number is even, otherwise answer "no"' ) ;
3+ const rules = 'Answer "yes" if the number is even, otherwise answer "no"'
64
7- export function evengame ( ) {
8- let correctAnswers = 0 ;
9- for ( let i = 0 ; i < 3 ; i ++ ) {
10-
11- const number = Math . floor ( Math . random ( ) * ( 52 ) ) ;
12- console . log ( `Question: ${ number } ` ) ;
13- const answer = readlineSync . question ( 'Your answer: ' ) ;
14- if ( number % 2 === 0 ) {
15- if ( answer === "yes" ) {
16- correctAnswers ++ ;
17- console . log ( 'Correct!' ) ;
18- } else {
19- console . log ( 'Wrong answer. Try again.' )
20- break
21- }
22- }
23- if ( number % 2 !== 0 ) {
24- if ( answer === "no" ) {
25- correctAnswers ++ ;
26- console . log ( 'Correct!' ) ;
27- } else {
28- console . log ( 'Wrong answer. Try again.' )
29- break
30- }
31- }
32- if ( correctAnswers === 3 ) {
33- console . log ( `Congratulations, ${ name } !` ) ;
34- }
35- }
36- }
5+ export function isEven ( ) {
6+ const number = Math . floor ( Math . random ( ) * ( 52 ) ) ;
7+ console . log ( `Question: ${ number } ` ) ;
8+ const answer = readlineSync . question ( 'Your answer: ' ) ;
9+ let result ;
10+ if ( number % 2 === 0 ) {
11+ result = 'yes'
12+ } else result = 'no' ;
13+
14+ return [ result , answer , result === answer ]
15+ }
16+ export const evenGame = ( ) => gamelogic ( rules , isEven ) ;
0 commit comments