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

Solution #431

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
23 changes: 13 additions & 10 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
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';

// Write your code here
const readline = require('readline');

const { getBullsAndCows } = require('./modules/getBullsAndCows');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { generateRandomNumber } = require('./modules/generateRandomNumber');

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

function tryGuess() {
terminal.question('Enter a 4-digit number: ', (userInput) => {
const userNumber = Number(userInput.trim());
const isValid = checkIsValidUserInput(userNumber);

if (isValid) {
if (userNumber === randomNumber) {
// eslint-disable-next-line no-console
console.log('Congratulations! You guessed the number.');

terminal.close();

return;
} else {
const result = getBullsAndCows(userNumber, randomNumber);

// eslint-disable-next-line no-console
console.log(`Result: \nbulls: ${result.bulls} \ncows: ${result.cows}`);
}
}

return tryGuess();
});
}

tryGuess();
27 changes: 26 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,32 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
if (isNaN(userInput)) {
// eslint-disable-next-line no-console
console.log('Invalid input. Please enter a 4-digit number.');

return false;
}

const numbers = String(userInput)
.split('')
.map((n) => Number(n));

if (numbers[0] === 0 || numbers.length !== 4) {
// eslint-disable-next-line no-console
console.log('Error. Enter a 4-digit number that doesnt start with 0.');

return false;
}

if (new Set(numbers).size !== numbers.length) {
// eslint-disable-next-line no-console
console.log('Error. Enter a 4-digit number that has only unique digits.');

return false;
}

return true;
}

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 digits = [];

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

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

if (!digits.includes(randomDigit)) {
digits.push(randomDigit);
}
}

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

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

const userArray = String(userInput).split('');
const guessArray = String(numberToGuess).split('');

for (let i = 0; i < userArray.length; i++) {
if (guessArray.includes(userArray[i])) {
if (guessArray[i] === userArray[i]) {
bulls++;
} else {
cows++;
}
}
}

return {
bulls,
cows,
};
}

module.exports = {
Expand Down
Loading