Skip to content

Commit aa5f7e0

Browse files
rootroot
authored andcommitted
add new Game - Game Calculate
1 parent a24c3ee commit aa5f7e0

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

bin/brain-calc.js

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

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import readlineSync from "readline-sync"
2+
export const needAnswers = 3;
3+
function TheMotor(feature, description) {
4+
console.log('Welcome to the Brain Games!');
5+
const name = readlineSync.question('May I have your name? ');
6+
console.log(`Hello, ${name}!`);
7+
console.log(description);
8+
for (let i = 1; i <=needAnswers; i+=1){
9+
const [exp, rightAnswer] = feature();
10+
console.log(`Question: ${exp} `);
11+
const userAnswer = readlineSync.question('Your Answer: ');
12+
if (userAnswer !== rightAnswer) {
13+
console.log(`'${userAnswer}' is wrong answer ;(. Correct answer was '${rightAnswer}'. \nLet's try again, ${name}!`);
14+
return;
15+
}
16+
console.log('Correct!');
17+
if (i === needAnswers) {
18+
console.log(`Congratulations, ${name}!`);
19+
return;
20+
}
21+
}
22+
}
23+
export {TheMotor};

src/BrainCacl.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { TheMotor } from "../index.js";
2+
import { getRandomNum } from "./brainEven.js";
3+
const alert = 'What is the result of the expression?';
4+
const operations = ()=>{
5+
const maxVal = 30;
6+
const minVal = 1;
7+
const firstNum = getRandomNum(minVal, maxVal);
8+
const secondNum = getRandomNum(maxVal,maxVal);
9+
const ops = ["+", "-", "*"];
10+
const minIndex = 0;
11+
const MaxIndex = ops.length-1;
12+
const randomOp = ops(getRandomNum(minIndex, MaxIndex));
13+
return `${firstNum}${randomOp}${secondNum}`
14+
}
15+
const StartValueOPS = (exs)=>{
16+
const convert = exs.split(' ');
17+
const num1 = +convert[0];
18+
const num2 = +convert[convert.length - 1];
19+
const ops = convert[1];
20+
let result = 0;
21+
22+
switch (ops) {
23+
case '+': result = num1 + num2;
24+
break;
25+
case '-': result = num1 - num2;
26+
break;
27+
case '*': result = num1 * num2;
28+
break;
29+
default: throw new Error(`Unknown order state: '${exs}'!`);
30+
}
31+
32+
return result;
33+
}
34+
const gameCalc = ()=>{
35+
const ops = operations();
36+
const rightAnswer = StartValueOPS(ops);
37+
return [ops, rightAnswer];
38+
}
39+
const ReadyGameCalc = ()=>{
40+
TheMotor(gameCalc, alert)
41+
}
42+
export default ReadyGameCalc;

src/brainEven.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
1-
import readlineSync from 'readline-sync';
1+
2+
import { TheMotor } from '../index.js';
23
const getRandomNum = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
3-
const needAnswers = 3
4-
function 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 <=needAnswers; 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-
}
4+
5+
246
const alert = '"yes" if the number is even, otherwise answer "no".';
257
const Game = ()=>{
268
const minNum = 1;
@@ -33,6 +15,6 @@ const Game = ()=>{
3315
const gameven = () =>{
3416
TheMotor(Game, alert)
3517
};
36-
export {gameven, TheMotor}
3718

19+
export {getRandomNum, gameven};
3820

0 commit comments

Comments
 (0)