Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sulution #413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
21 changes: 20 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/* eslint-disable no-console */
'use strict';

// Write your code here
const readline = require('readline');
const { generateRandomNumber } = require('./modules/generateRandomNumber');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { getBullsAndCows } = require('./modules/getBullsAndCows');

const terminal = readline.createInterface(process.stdin, process.stdout);

terminal.question('Please quess the number', (number) => {
const randomNum = generateRandomNumber();

if (!checkIsValidUserInput(number)) {
console.log('your input do not valid');
terminal.close();
}

console.log(getBullsAndCows(number, randomNum));

terminal.close();
});
15 changes: 14 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
if (userInput.startsWith('0')) {
return false;
}

if (userInput.length !== 4) {
return false;
}

const uniqueValues = new Set(userInput.split(''));

return (
Array.from(uniqueValues.values()).filter((i) => !Number.isNaN(Number(i)))
.length === 4
);
}

module.exports = {
Expand Down
18 changes: 17 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const randomNumbers = [];
const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

while (randomNumbers.length < 4) {
let index;
const randomNumber = Math.random() * (digits.length - 1);

if (!randomNumbers.length) {
index = Math.ceil(randomNumber);
} else {
index = Math.floor(randomNumber);
}

randomNumbers.push(digits.splice(index, 1));
}

return Number(randomNumbers.join(''));
}

module.exports = {
Expand Down
20 changes: 19 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const result = { bulls: 0, cows: 0 };
const userInputArr = String(userInput).split('');
const numberToGuessArr = String(numberToGuess).split('');

for (let i = 0; i < 4; i++) {
const userNum = userInputArr[i];
const guessNum = numberToGuessArr[i];

if (userNum === guessNum) {
result.bulls++;
continue;
}

if (numberToGuessArr.includes(userNum)) {
result.cows++;
}
}

return result;
}

module.exports = {
Expand Down
Loading