Skip to content

Commit

Permalink
implemented prime game
Browse files Browse the repository at this point in the history
  • Loading branch information
akivonen committed Apr 17, 2024
1 parent b9229cc commit 4b8a3a7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ brain-gcd:
node bin/brain-gcd.js
brain-progression:
node bin/brain-progression.js
brain-prime:
node bin/brain-prime.js
make lint:
npx eslint .
publish:
Expand Down
6 changes: 6 additions & 0 deletions bin/brain-prime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

import app from '../src/index.js';
import prime from '../src/prime.js';

app(prime);
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"brain-even": "bin/brain-even.js",
"brain-calc": "bin/brain-calc.js",
"brain-gcd": "bin/brain-gcd.js",
"brain-progression": "bin/brain-progression.js"
"brain-progression": "bin/brain-progression.js",
"brain-prime": "bin/brain-prime.js"
},
"main": "index.js",
"scripts": {
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default function app(game, questionsCount = countOfQuestions) {
for (let i = 0; i < questionsCount; i += 1) {
const { question, correctAnswer } = questionsAnswers[i];
console.log(`Question: ${question}`);
console.log(correctAnswer);
const answer = readlineSync.question('Your answer: ');
if (answer !== correctAnswer) {
console.log(`${answer} is wrong answer ;(. Correct answer was ${correctAnswer}.
Expand Down
27 changes: 27 additions & 0 deletions src/prime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import randomize from './utils/randomize.js';

const isPrime = (num) => {
if (num < 2) {
return false;
}
const halfNum = Math.floor(num / 2);
for (let i = halfNum; i > 1; i -= 1) {
if (num % i === 0) {
return false;
}
}
return true;
};
export default function prime(questionCount = 3) {
const rules = 'Answer "yes" if given number is prime. Otherwise answer "no".';
const questionsAnswers = [];
for (let i = 0; i < questionCount; i += 1) {
const number = randomize(0, 100);
const correctAnswer = isPrime(number) ? 'yes' : 'no';
questionsAnswers.push({
question: number,
correctAnswer,
});
}
return { rules, questionsAnswers };
}

0 comments on commit 4b8a3a7

Please sign in to comment.