Skip to content

Commit

Permalink
inquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
mrsvolodya committed Sep 24, 2024
1 parent b32106e commit 48d3b23
Show file tree
Hide file tree
Showing 11 changed files with 2,154 additions and 210 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
File renamed without changes.
2,232 changes: 2,073 additions & 159 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "Bulls and cows game",
"main": "src/app.js",
"type": "module",
"scripts": {
"init": "mate-scripts init",
"start": "mate-scripts start",
Expand All @@ -17,15 +18,22 @@
"author": "Mate academy",
"license": "GPL-3.0",
"devDependencies": {
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.9.12",
"babel-jest": "^29.7.0",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
"figlet": "^1.7.0",
"jest": "^29.7.0",
"prettier": "^3.3.2"
},
"mateAcademy": {
"projectType": "javascript"
},
"dependencies": {
"inquirer": "^11.0.2"
}
}
90 changes: 60 additions & 30 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,72 @@
/* eslint-disable no-console */
'use strict';
import inquirer from 'inquirer';
import chalk from 'chalk';
import checkIsValidUserInput from './modules/checkIsValidUserInput.js';
import getBullsAndCows from './modules/getBullsAndCows.js';

const readline = require('node:readline');
const terminal = readline.createInterface(process.stdin, process.stdout);
const { getBullsAndCows } = require('./modules/getBullsAndCows');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
inquirer
.prompt([
{
type: 'input',
name: 'guessedNumbers',
message: "Let's start game! Write four numbers in field:\n",
validate: (result) => {
const check = checkIsValidUserInput(result);

async function runTerminal() {
while (true) {
const numbers = await new Promise((resolve) => {
terminal.question(
"Let's start game! Write four numbers in field:\n",
resolve,
);
});
if (!check) {
return 'Please try again';
}

const check = await checkIsValidUserInput(numbers);
return true;
},
},
])
.then((answers) => {
console.log(
chalk.green('Your result is:'),
getBullsAndCows(answers.guessedNumbers),
);
});

if (!check) {
process.stdout.write('False, check numbers, try again!\n');
// const readline = require('node:readline');

continue;
}
// const terminal = inquirer.createInterface(process.stdin, process.stdout);
// const { getBullsAndCows } = require('./modules/getBullsAndCows');
// const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');

const result = await getBullsAndCows(numbers);
const resStr = JSON.stringify(result);
// async function runTerminal() {
// while (true) {
// const numbers = await new Promise((resolve) => {
// terminal.question(
// "Let's start game! Write four numbers in field:\n",
// resolve,
// );
// });

process.stdout.write(`Result is ${resStr}\n`);
// const check = await checkIsValidUserInput(numbers);

const continueGame = await new Promise((resolve) => {
terminal.question('Do you want to continue GAME, Yes/No ?', resolve);
});
// if (!check) {
// process.stdout.write('False, check numbers, try again!\n');

if (continueGame.toLowerCase() === 'no') {
break;
}
}
// continue;
// }

terminal.close();
}
// const result = await getBullsAndCows(numbers);
// const resStr = JSON.stringify(result);

runTerminal();
// process.stdout.write(`Result is ${resStr}\n`);

// const continueGame = await new Promise((resolve) => {
// terminal.question('Do you want to continue GAME, Yes/No ?', resolve);
// });

// if (continueGame.toLowerCase() === 'no') {
// break;
// }
// }

// terminal.close();
// }

// runTerminal();
6 changes: 1 addition & 5 deletions src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @param {string} userInput - The user input
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
export default function checkIsValidUserInput(userInput) {
const usersNum = String(userInput).split('');
const unic = new Set(userInput);

Expand All @@ -23,7 +23,3 @@ function checkIsValidUserInput(userInput) {

return true;
}

module.exports = {
checkIsValidUserInput,
};
6 changes: 1 addition & 5 deletions src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
export default function generateRandomNumber() {
const n = new Set();

while (n.size < 4) {
Expand All @@ -15,7 +15,3 @@ function generateRandomNumber() {

return +Array.from(n).join('');
}

module.exports = {
generateRandomNumber,
};
11 changes: 5 additions & 6 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { generateRandomNumber } = require('./generateRandomNumber');
import generateRandomNumber from './generateRandomNumber.js';

/**
* Calculate the number of bulls and cows for a given user input.
Expand All @@ -14,7 +14,10 @@ const { generateRandomNumber } = require('./generateRandomNumber');
* @return {object} An object containing the number of bulls and cows.
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess = generateRandomNumber()) {
export default function getBullsAndCows(
userInput,
numberToGuess = generateRandomNumber(),
) {
const convert = (v) => String(v).split('');
const result = {
bulls: 0,
Expand All @@ -33,7 +36,3 @@ function getBullsAndCows(userInput, numberToGuess = generateRandomNumber()) {

return result;
}

module.exports = {
getBullsAndCows,
};
4 changes: 1 addition & 3 deletions tests/checkIsValidUserInput.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const {
checkIsValidUserInput,
} = require('../src/modules/checkIsValidUserInput');
import checkIsValidUserInput from '../src/modules/checkIsValidUserInput.js';

describe('checkIsValidUserInput', () => {
test('returns false for non-4-digit inputs', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/generateRandomNumber.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { generateRandomNumber } = require('../src/modules/generateRandomNumber');
import generateRandomNumber from '../src/modules/generateRandomNumber.js';

describe('generateRandomNumber', () => {
test('should return a number', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/getBullsAndCows.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { getBullsAndCows } = require('../src/modules/getBullsAndCows');
import getBullsAndCows from '../src/modules/getBullsAndCows.js';

describe('getBullsAndCows', () => {
test('returns 0 bulls and 0 cows when there are no matches', () => {
Expand Down

0 comments on commit 48d3b23

Please sign in to comment.