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

add task solution #423

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@
'use strict';

// Write your code here
import readline from 'readline';
import { generateRandomNumber } from './modules/generateRandomNumber';
import { checkIsValidUserInput } from './modules/checkIsValidUserInput';
import { getBullsAndCows } from './modules/getBullsAndCows';

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

const numberToGuess = generateRandomNumber();

terminal.question('Guess the number', (userNumber) => {
if (!checkIsValidUserInput(userNumber)) {
return;

Choose a reason for hiding this comment

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

The program should provide feedback to the user if the input is invalid. Consider adding a message to inform the user that their input was not valid.

}

if (userNumber === numberToGuess) {

Choose a reason for hiding this comment

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

You should provide feedback to the user when they guess the number correctly, such as a congratulatory message.

terminal.close();

Choose a reason for hiding this comment

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

After closing the terminal, it might be a good idea to inform the user that they have guessed the correct number. Consider adding a message to congratulate the user.

} else {
getBullsAndCows(userNumber, numberToGuess);

Choose a reason for hiding this comment

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

The result of getBullsAndCows is not being used. Consider storing the result and providing feedback to the user about the number of bulls and cows.

Choose a reason for hiding this comment

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

The result of getBullsAndCows is not being used. You might want to display the bulls and cows count to the user.

}
});
18 changes: 17 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
if (userInput.length !== 4) {
return false;
}

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

const arr = [...userInput];

for (const num of arr) {
if (arr.filter((number) => number === num).length > 1 || isNaN(+num)) {
Comment on lines +22 to +23

Choose a reason for hiding this comment

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

The current logic checks for duplicate digits by filtering the array for each digit. This results in a time complexity of O(n^2), which is inefficient for this task. Consider using a Set to track seen digits, which will improve the efficiency of the duplicate check.

Choose a reason for hiding this comment

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

The condition 'isNaN(+num)' is used to check if the character is a number, but this is unnecessary since the input is expected to be a string of digits. If you want to ensure the input consists only of digits, consider using a regular expression at the beginning of the function.

Comment on lines +22 to +23

Choose a reason for hiding this comment

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

The current logic checks for duplicate digits by filtering the array for each digit and checking the length. This approach is inefficient. Consider using a Set to track seen digits, which would simplify the logic and improve performance.

return false;
}
}

return true;
}

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

while (numArr.length < 4) {
const newNum = Math.floor(Math.random() * 9);

Choose a reason for hiding this comment

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

The range for generating 'newNum' should be from 0 to 9, but the current implementation only generates numbers from 0 to 8. To include 9, the multiplication factor should be 10 instead of 9.

Choose a reason for hiding this comment

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

The range for generating newNum should be from 0 to 9 inclusive, but Math.floor(Math.random() * 9) generates numbers from 0 to 8. To include 9, you should use Math.floor(Math.random() * 10).


if (!numArr.includes(newNum)) {
numArr.push(newNum);
}
}

return +numArr.join('');
}

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

const userArr = [...String(userInput)];
const guessArr = [...String(numberToGuess)];

for (let i = 0; i < userArr.length; i++) {
if (guessArr[i] === userArr[i]) {
score.bulls += 1;
} else if (guessArr.includes(userArr[i])) {
score.cows += 1;
Comment on lines +27 to +28

Choose a reason for hiding this comment

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

There is a logical issue here. The current logic increments cows even if the digit has already been counted as a bull. You need to ensure that a digit is not counted as a cow if it has already been counted as a bull.

Comment on lines +27 to +28

Choose a reason for hiding this comment

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

The current logic for counting cows is incorrect because it counts a cow even if the digit has already been counted as a bull. You should ensure that a digit is not counted as a cow if it has already been counted as a bull.

}
}

return score;
}

module.exports = {
Expand Down
Loading