Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykyta Romanishyn committed Dec 19, 2024
1 parent b9eb4f1 commit bb44500
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
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
24 changes: 24 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
'use strict';

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

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const generatedNumber = generateRandomNumber();

rl.question('Enter your number: ', (userInput) => {
if (checkIsValidUserInput(userInput)) {
// eslint-disable-next-line no-console
console.log(getBullsAndCows(userInput, generatedNumber));
} else {
// eslint-disable-next-line no-console
console.log(`Your number is not valid`);
}
rl.close();
});
10 changes: 9 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
if (
userInput[0] === '0' ||
new Set(userInput).size !== 4 ||
isNaN(userInput)
) {
return false;
}

return true;
}

module.exports = {
Expand Down
8 changes: 7 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const randomNumber = Math.floor(Math.random() * (10000 - 1) + 1);

if (new Set(randomNumber.toString()).size !== 4) {
return generateRandomNumber();
} else {
return randomNumber;
}
}

module.exports = {
Expand Down
17 changes: 16 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const userInputStr = userInput.toString();
const numberToGuessStr = numberToGuess.toString();
const result = { bulls: 0, cows: 0 };

for (let i = 0; i < numberToGuessStr.length; i++) {
if (userInputStr[i] === numberToGuessStr[i]) {
result.bulls += 1;
continue;
}

if (numberToGuessStr.includes(userInputStr[i])) {
result.cows += 1;
}
}

return result;
}

module.exports = {
Expand Down

0 comments on commit bb44500

Please sign in to comment.