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 #425

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
35 changes: 35 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
'use strict';

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

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

function playGame() {
const secretNumber = generateRandomNumber().toString();

function askGuess() {
terminal.question('Enter your number: ', (userInput) => {
if (!checkIsValidUserInput(userInput)) {
return askGuess();
}

const { bulls } = getBullsAndCows(secretNumber, userInput);
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 code correctly calculates the number of bulls and cows, but it does not provide any feedback to the user. Consider adding a console.log statement to inform the user about the number of bulls and cows after each guess.


if (bulls === 4) {
terminal.close();
} else {
askGuess();
}
});
}

askGuess();
}

module.exports = {
playGame,
};
18 changes: 18 additions & 0 deletions src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */

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

const numbers = userInput.split('');

if (!numbers.every((num) => /^\d$/.test(num))) {
return false;
}

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

const uniqueNumber = new Set(numbers);

return uniqueNumber.size === 4;
}

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

numbers.push(Math.floor(Math.random() * 9) + 1);

while (numbers.length < 4) {
const randomNumber = Math.floor(Math.random() * 10);

if (!numbers.includes(randomNumber)) {
numbers.push(randomNumber);
}
}

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

module.exports = {
Expand Down
20 changes: 20 additions & 0 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const userInputStr = userInput.toString();
const numberToGuessStr = numberToGuess.toString();

let bulls = 0;
let cows = 0;

for (let i = 0; i < 4; i++) {
if (userInputStr[i] === numberToGuessStr[i]) {
bulls++;
}

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

return { bulls, cows };
}

module.exports = {
Expand Down
Loading