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

finished #435

Open
wants to merge 3 commits 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
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module.exports = {
extends: '@mate-academy/eslint-config',
env: {
jest: true
jest: true,
},
rules: {
'no-proto': 0
'no-proto': 'warn',
},
plugins: ['jest']
plugins: ['jest'],
};
17 changes: 8 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ name: Test

on:
pull_request:
branches: [ master ]
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
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using 'actions/setup-node@v3' instead of 'v1' to ensure you are using the latest features and security updates.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider updating the 'setup-node' action to 'v3' to benefit from the latest features and security updates.

with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
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
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ Implement `Bulls and cows` game so the user can run and play it using command li
- `cow` - guessed digit exists in the number but the place (index) is wrong
- the game ends when the numbers is found
- write a solution using separate modules for generating a number, calculating bulls and cows, validate user input, input/output operations
**(corresponding files are already created)**
**(corresponding files are already created)**
- use `npm run test` command to test your modules
- use `npm run play` to run the game

## Example

Computer makes `1234`, user prints `1345`. The result is one `bull` (guessed
digit `1` is on it's place) and 2 `cows` (digits `3` and `4` are present but on
wrong places).
36 changes: 35 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
'use strict';
import readline from 'node:readline';
import { generateRandomNumber } from './modules/generateRandomNumber.js';
import { checkIsValidUserInput } from './modules/checkIsValidUserInput.js';
import { getBullsAndCows } from './modules/getBullsAndCows.js';

// Write your code here
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const hiddenNumber = generateRandomNumber();

const requestANumber = () => {
// console.log(hiddenNumber);

rl.question('Write your number? ', (userValue) => {
const isValidUserInput = checkIsValidUserInput(userValue);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that the checkIsValidUserInput function correctly validates the input. The checkIsUniqueNumbers function currently returns true prematurely, which can lead to incorrect validation results. Make sure it checks all digits for uniqueness before returning true.


if (!isValidUserInput) {
requestANumber();

return;
}

const bullsAndCows = getBullsAndCows(userValue, hiddenNumber);

if (bullsAndCows.bulls === 4) {
rl.close();

return;
}

requestANumber();
});
};

requestANumber();
55 changes: 55 additions & 0 deletions src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,63 @@
* @param {string} userInput - The user input
* @return {boolean} - True if the user input is valid, false otherwise
*/
const checkIsUniqueNumbers = (value) => {
const arrayValue = `${value}`.split('');
// console.log(arrayValue.length);

if (arrayValue.length !== 4) {
// console.log('(arrayValue.length !== 4)');

return false;
}

if (typeof +value !== 'number') {
// console.log(`(typeof value !== 'number')`);

return false;
}

if (arrayValue[0] === '0') {
// console.log(`(arrayValue[0] === '0')`);

return false;
}

const arrayValueToCheck = [...arrayValue];

for (let i = 0; i < arrayValue.length; i++) {
const numberToCheck = arrayValue[i];

arrayValueToCheck.splice(i, 1);

if (arrayValueToCheck.includes(numberToCheck)) {
// console.log(`arrayValueToCheck.includes(numberToCheck)`);

return false;
}

return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return true; statement is inside the loop, causing the function to return true after checking only the first digit. Move this statement outside the loop to ensure all digits are checked for uniqueness before returning true.

}
};

function checkIsValidUserInput(userInput) {
/* Write your code here */

const numberedUserInput = +userInput;

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

if (typeof numberedUserInput !== 'number' || isNaN(numberedUserInput)) {
return false;
}

if (+userInput[0] === 0) {
return false;
}

return checkIsUniqueNumbers(userInput);
}

module.exports = {
Expand Down
11 changes: 11 additions & 0 deletions src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const generatedNumsArr = [];

for (let i = 0; i < 4; i++) {
const pickingNumberIndex = Math.floor(Math.random() * nums.length);

generatedNumsArr.push(nums[pickingNumberIndex]);
nums.splice(pickingNumberIndex, 1);
}
/* Write your code here */

return +generatedNumsArr.join('');
}

module.exports = {
Expand Down
23 changes: 23 additions & 0 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const userInputArr = userInput.toString().split('');
const numberToGuessArr = numberToGuess.toString().split('');
const result = { bulls: 0, cows: 0 };

for (let i = 0; i < userInputArr.length; i++) {
if (userInputArr[i] === numberToGuessArr[i]) {
result.bulls += 1;

continue;
}

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

// if (result.bulls === 4) {
// console.log('you won!');

// return;
// }

return result;
}

module.exports = {
Expand Down
Loading