Skip to content

Commit bfc6efd

Browse files
committed
done step 5
1 parent dc7433e commit bfc6efd

File tree

7 files changed

+145
-1
lines changed

7 files changed

+145
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
[![Actions Status](https://github.com/Michael57e/qa-auto-engineer-javascript-project-44/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/Michael57e/qa-auto-engineer-javascript-project-44/actions)
33

44
[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=Michael57e_qa-auto-engineer-javascript-project-44&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=Michael57e_qa-auto-engineer-javascript-project-44)
5+
6+
## Brain Even
7+
![asciicast](https://asciinema.org/a/xnGjhZ5kQXaHREkfxL1gQThiR.svg)](https://asciinema.org/a/xnGjhZ5kQXaHREkfxL1gQThiR)
8+

bin/brain-even.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
import runBrainEven from '../src/games/even.js';
4+
5+
runBrainEven();

demo.cast

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{"version": 2, "width": 108, "height": 34, "timestamp": 1756976478, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.229466, "o", "\u001b[?2004h\u001b]0;michael@michael-Nitro-ANV15-51: ~/qa-auto-engineer-javascript-project-44\u0007\u001b[01;32mmichael@michael-Nitro-ANV15-51\u001b[00m:\u001b[01;34m~/qa-auto-engineer-javascript-project-44\u001b[00m$ "]
3+
[4.674279, "o", "b"]
4+
[5.135747, "o", "r"]
5+
[5.395008, "o", "a"]
6+
[5.661149, "o", "i"]
7+
[6.094325, "o", "n"]
8+
[6.951184, "o", "-"]
9+
[7.27935, "o", "e"]
10+
[7.572577, "o", "v"]
11+
[7.701985, "o", "e"]
12+
[8.330934, "o", "n"]
13+
[9.434169, "o", "\r\n"]
14+
[9.434262, "o", "\u001b[?2004l\r"]
15+
[9.468932, "o", "Welcome to the Brain Games!\r\n"]
16+
[9.469009, "o", "Answer \"yes\" if the number is even, otherwise answer \"no\".\r\n"]
17+
[9.469821, "o", "May I have your name? "]
18+
[11.755813, "o", "M"]
19+
[12.094951, "o", "i"]
20+
[13.001409, "o", "c"]
21+
[13.281536, "o", "h"]
22+
[13.493008, "o", "a"]
23+
[13.825063, "o", "e"]
24+
[14.287943, "o", "l"]
25+
[15.360404, "o", "\r\n"]
26+
[15.361699, "o", "Hello, Michael!\r\n"]
27+
[15.362156, "o", "Question: 99\r\nYour answer: "]
28+
[18.16029, "o", "n"]
29+
[18.369097, "o", "o"]
30+
[19.509295, "o", "\r\n"]
31+
[19.509864, "o", "Correct!\r\n"]
32+
[19.510291, "o", "Question: 78\r\nYour answer: "]
33+
[21.413909, "o", "y"]
34+
[21.574036, "o", "e"]
35+
[21.797901, "o", "s"]
36+
[22.284641, "o", "\r\n"]
37+
[22.28516, "o", "Correct!\r\nQuestion: 59\r\n"]
38+
[22.285355, "o", "Your answer: "]
39+
[23.675315, "o", "n"]
40+
[23.812767, "o", "o"]
41+
[24.56712, "o", "\r\n"]
42+
[24.567418, "o", "Correct!\r\n"]
43+
[24.56812, "o", "Congratulations, Michael!\r\n"]
44+
[24.578296, "o", "\u001b[?2004h\u001b]0;michael@michael-Nitro-ANV15-51: ~/qa-auto-engineer-javascript-project-44\u0007\u001b[01;32mmichael@michael-Nitro-ANV15-51\u001b[00m:\u001b[01;34m~/qa-auto-engineer-javascript-project-44\u001b[00m$ "]
45+
[26.543131, "o", "b"]
46+
[27.002617, "o", "r"]
47+
[27.274257, "o", "a"]
48+
[27.55633, "o", "i"]
49+
[27.879222, "o", "n"]
50+
[28.986899, "o", "-"]
51+
[29.344938, "o", "e"]
52+
[29.626476, "o", "v"]
53+
[29.764281, "o", "e"]
54+
[30.117185, "o", "n"]
55+
[30.797402, "o", "\r\n\u001b[?2004l\r"]
56+
[30.836242, "o", "Welcome to the Brain Games!\r\n"]
57+
[30.836361, "o", "Answer \"yes\" if the number is even, otherwise answer \"no\".\r\n"]
58+
[30.837456, "o", "May I have your name? "]
59+
[33.517794, "o", "M"]
60+
[33.840488, "o", "i"]
61+
[34.183684, "o", "c"]
62+
[34.774634, "o", "h"]
63+
[34.956579, "o", "a"]
64+
[35.24936, "o", "e"]
65+
[35.546723, "o", "l"]
66+
[36.620801, "o", "\r\n"]
67+
[36.621903, "o", "Hello, Michael!\r\n"]
68+
[36.622224, "o", "Question: 40\r\nYour answer: "]
69+
[38.678876, "o", "y"]
70+
[38.989352, "o", "e"]
71+
[39.267041, "o", "s"]
72+
[39.959654, "o", "\r\n"]
73+
[39.960373, "o", "Correct!\r\nQuestion: 33\r\n"]
74+
[39.960727, "o", "Your answer: "]
75+
[41.7383, "o", "y"]
76+
[42.043042, "o", "e"]
77+
[42.331875, "o", "s"]
78+
[43.255535, "o", "\r\n"]
79+
[43.256036, "o", "'yes' is wrong answer ;(. Correct answer was 'no'.\r\nLet's try again, Michael!\r\n"]
80+
[43.263674, "o", "\u001b[?2004h\u001b]0;michael@michael-Nitro-ANV15-51: ~/qa-auto-engineer-javascript-project-44\u0007\u001b[01;32mmichael@michael-Nitro-ANV15-51\u001b[00m:\u001b[01;34m~/qa-auto-engineer-javascript-project-44\u001b[00m$ "]
81+
[49.723721, "o", "\u001b[?2004l\r\r\n"]
82+
[49.724047, "o", "exit\r\n"]

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"test": "echo \"Error: no test specified\" && exit 1"
1919
},
2020
"bin": {
21-
"brain-games": "bin/brain-games.js"
21+
"brain-games": "bin/brain-games.js",
22+
"brain-even": "bin/brain-even.js"
2223
},
2324
"dependencies": {
2425
"readline-sync": "^1.4.10"

src/games/even.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import runEngine from '../index.js'
2+
import getrandomNumber from '../utils.js'
3+
4+
const rule = 'Answer "yes" if the number is even, otherwise answer "no".'
5+
6+
const isEven = (num) => {
7+
return num % 2 === 0
8+
}
9+
10+
const getQuestionAndAnswer = () => {
11+
const question = getrandomNumber(1, 100)
12+
const correctAnswer = isEven(question) ? 'yes' : 'no'
13+
return [question, correctAnswer]
14+
}
15+
16+
export default () => runEngine(rule, getQuestionAndAnswer)

src/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import readlineSync from 'readline-sync'
2+
3+
const roundCount = 3
4+
5+
const runEngine = (rule, getQuestionAndAnswer) => {
6+
console.log('Welcome to the Brain Games!')
7+
console.log(rule)
8+
9+
const name = readlineSync.question('May I have your name? ')
10+
console.log(`Hello, ${name}!`)
11+
12+
for (let i = 0; i < roundCount; i += 1) {
13+
const [question, correctAnswer] = getQuestionAndAnswer()
14+
console.log(`Question: ${question}`)
15+
const userAnswer = readlineSync.question('Your answer: ')
16+
17+
if (userAnswer !== correctAnswer) {
18+
console.log(`'${userAnswer}' is wrong answer ;(. Correct answer was '${correctAnswer}'.`)
19+
console.log (`Let's try again, ${name}!`)
20+
return
21+
}
22+
23+
console.log('Correct!')
24+
}
25+
26+
console.log(`Congratulations, ${name}!`)
27+
}
28+
29+
export default runEngine

src/utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const getRandomNumber = (min, max) => {
2+
const minNum = Math.ceil(min);
3+
const maxNum = Math.floor(max);
4+
return Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;
5+
};
6+
7+
export default getRandomNumber;

0 commit comments

Comments
 (0)