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 solution #352

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
33 changes: 32 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
'use strict';

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

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

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

function askForNumber() {
terminal.question('Enter a number of 4 different digits ', (digits) => {
if (checkIsValidUserInput(digits)) {
const result = getBullsAndCows(digits, randomNum);

process.stdout.write(`Result: ${JSON.stringify(result)}\n`);

if (result.bulls === 4) {
terminal.close();
process.stdout.write(`You guessed all numbers. Game is over!`);
} else {
askForNumber();
}
} else {
process.stdout.write(
`You entered incorrect numbers. Please try again.\n`,
);
askForNumber();
}
});
}

askForNumber();
19 changes: 18 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const userInputArr = userInput.split('').map((el) => Number(el));
const allowedLength = 4;
let isValid = true;

if (
userInputArr.length !== allowedLength ||
new Set(userInputArr).size !== allowedLength
) {
return false;
}

userInputArr.forEach((el, i, arr) => {
if (isNaN(el) || arr[0] === 0) {
isValid = false;
}
});

return isValid;
}

module.exports = {
Expand Down
13 changes: 12 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
*
* @return {number} A random 4-digit number
*/

const getRandomNum = () => {
return Math.floor(Math.random() * 9000) + 1000;
};

function generateRandomNumber() {
/* Write your code here */
let res;

do {
res = getRandomNum();
} while (new Set(String(res).split('')).size !== 4);

Choose a reason for hiding this comment

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

Try to make up better logic for generating random number, because now this loop can be repeated a lot of times


return res;
}

module.exports = {
Expand Down
25 changes: 24 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,31 @@
* @return {object} An object containing the number of bulls and cows.
* Example: { bulls: 1, cows: 2 }
*/

const createArrFromNum = (num) => {
return String(num)
.split('')
.map((el) => Number(el));
};

function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const userInputArr = createArrFromNum(userInput);
const numberToGuessArr = createArrFromNum(numberToGuess);

const result = {
bulls: 0,
cows: 0,
};

userInputArr.forEach((item, index) => {
if (item === numberToGuessArr[index]) {
result.bulls++;
} else if (numberToGuessArr.includes(item)) {
result.cows++;
}
});

return result;
}

module.exports = {
Expand Down
Loading