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

impl solution #347

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

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

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

const chooseNumber = () =>
terminal.question('Enter the 4-digit unique number: ', (input) => {
const generatedNumber = generateRandomNumber();
const isValidInput = checkIsValidUserInput(input);

if (!isValidInput) {
log('Invalid input. Try again.');
chooseNumber();

return;
}

if (input === generatedNumber) {
log('Congratulations! You won!');
terminal.close();

return;
}

const { bulls, cows } = getBullsAndCows(input, generatedNumber);

log(`Your result is: Bulls - ${bulls}, Cows - ${cows}`);
chooseNumber();
});

chooseNumber();
14 changes: 13 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const userInputArray = userInput.split('');
const isNumeric = userInputArray.every((item) => !isNaN(item));

if (
userInputArray.length !== 4 ||
userInputArray[0] === '0' ||
new Set(userInputArray).size !== 4 ||
!isNumeric
) {
return false;
}

return true;
}

module.exports = {
Expand Down
19 changes: 18 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,26 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
let { number, numberArray } = getRandomNumAndArray();

while (new Set(numberArray).size !== 4) {
const { number: numberRegen, numberArray: numberArrayRegen } =
getRandomNumAndArray();

number = numberRegen;
numberArray = numberArrayRegen;
}

return number;
}

const getRandomNumAndArray = () => {
const number = Math.floor(1000 + Math.random() * 9000);
const numberArray = number.toString().split('');

return { number, numberArray };
};

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

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

return { bulls, cows };
}

module.exports = {
Expand Down
Loading