Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisGurskiy committed Apr 12, 2024
1 parent 23243ad commit bda73ad
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
42 changes: 41 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
/* eslint-disable no-console */
'use strict';

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

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

const generatedDigits = generateRandomNumber();
let firstQuestion = false;

function askUser() {
terminal.question(
`${firstQuestion ? `Try again ` : `I've chosen a number of 4 different digits. Try to guess it: `}`,
(digits) => {
if (!checkIsValidUserInput(digits)) {
return false;
}

firstQuestion = true;

const { bulls, cows } = getBullsAndCows(digits, generatedDigits);

console.log(
`Your result: ${bulls > 0 ? `${bulls > 1 ? `${bulls} bulls` : `${bulls} bull`}` : ''}${bulls > 0 && cows > 0 ? ` and ` : ``}${cows > 0 ? `${cows > 1 ? `${cows} cows` : `${cows} cow`}` : ''}`,
);

if (bulls === 4) {
console.log('Congratulations. You guessed it.');
terminal.close();
} else {
askUser();
}
},
);
}

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

if (userInput < 1000 || userInput > 9999) {
return false;
}

const userInputString = userInput.toString();

for (let i = 0; i < userInputString.length; i++) {
for (let j = i + 1; j < userInputString.length; j++) {
if (userInputString[i] === userInputString[j]) {
return false;
}
}
}

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

return true;
}

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

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

if (digits.length === 0 && digit === 0) {
continue;
}

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

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

module.exports = {
Expand Down
19 changes: 18 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
'use strict';

/**
Expand All @@ -13,7 +14,23 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
let bulls = 0;
let cows = 0;

const userInputString = userInput.toString();
const numberToGuessString = numberToGuess.toString();

for (let i = 0; i < userInputString.length; i++) {
const digit = userInputString[i];

if (digit === numberToGuessString[i]) {
bulls++;
} else if (numberToGuessString.includes(digit)) {
cows++;
}
}

return { bulls, cows };
}

module.exports = {
Expand Down

0 comments on commit bda73ad

Please sign in to comment.