|
| 1 | +import readlineSync from 'readline-sync'; |
| 2 | +const getRandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); |
| 3 | +const needAnswers = 3 |
| 4 | +const TheMotor = (feature, description) => { |
| 5 | + console.log('Welcome to the Brain Games!'); |
| 6 | + const name = readlineSync.question('May I have your name? '); |
| 7 | + console.log(`Hello, ${name}!`); |
| 8 | + console.log(description); |
| 9 | + for (let i = 1; i <= correctAnswers; i += 1) { |
| 10 | + const [exp, rightAnswer] = feature(); |
| 11 | + console.log(`Question: ${exp} `); |
| 12 | + const userAnswer = readlineSync.question('Your Answer: '); |
| 13 | + if (userAnswer !== rightAnswer) { |
| 14 | + console.log(`'${userAnswer}' is wrong answer ;(. Correct answer was '${rightAnswer}'. \nLet's try again, ${name}!`); |
| 15 | + return; |
| 16 | + } |
| 17 | + console.log('Correct!'); |
| 18 | + if (i === needAnswers) { |
| 19 | + console.log(`Congratulations, ${name}!`); |
| 20 | + return; |
| 21 | + } |
| 22 | + } |
| 23 | + }; |
| 24 | +const alert = '"yes" if the number is even, otherwise answer "no".'; |
| 25 | +const Game = ()=>{ |
| 26 | + const minNum = 1; |
| 27 | + const maxNum = 1000; |
| 28 | + const randomNum = getRandomNum(minNum, maxNum); |
| 29 | + const rightAnswer = randomNum % 2 === 0 ? 'yes' : 'no'; |
| 30 | + const coll = [randomNum, rightAnswer]; |
| 31 | + return coll; |
| 32 | + |
| 33 | +} |
| 34 | +const gameven = () =>{ |
| 35 | + TheMotor(Game, alert) |
| 36 | +}; |
| 37 | +export {gameven, TheMotor} |
| 38 | + |
| 39 | + |
0 commit comments