Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmonnn committed Mar 23, 2024
1 parent 23243ad commit daccddf
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
'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 numberToGuess = generateRandomNumber();

function guess() {
terminal.question('Enter 4 different numbers: ', (data) => {
if (checkIsValidUserInput(data)) {
// eslint-disable-next-line no-console
console.log(getBullsAndCows(+data, +numberToGuess));
} else {
// eslint-disable-next-line no-console
console.log('Invalid input. Please enter 4 different numbers: ');
}

if (+data === numberToGuess) {
// eslint-disable-next-line no-console
console.log('You are a winner');
terminal.close();

return;
}

return guess();
});
}

guess();
20 changes: 20 additions & 0 deletions src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */

if (userInput.length !== 4 || Number(userInput[0]) === 0) {
return false;
}

const amount = {};

for (let i = 0; i < userInput.length; i++) {
if (isNaN(Number(userInput[i]))) {
return false;
}

if (!amount[userInput[i]]) {
amount[userInput[i]] = 1;
} else {
return false;
}
}

return true;
}

module.exports = {
Expand Down
22 changes: 22 additions & 0 deletions src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
*/
function generateRandomNumber() {
/* Write your code here */
const numberToGuess = [];

for (let i = 0; i < 4; ) {
const min = 0;
const max = 9;

const randmonNumber = Math.floor(Math.random() * (max - min + 1) + min);

if (!numberToGuess.includes(randmonNumber)) {
i++;

numberToGuess.push(randmonNumber);
}

continue;
}

if (numberToGuess[0] === 0) {
return generateRandomNumber();
}

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

module.exports = {
Expand Down
15 changes: 15 additions & 0 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
let bulls = 0;
let cows = 0;

const input = String(userInput);
const guess = String(numberToGuess);

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

return { bulls, cows };
}

module.exports = {
Expand Down

0 comments on commit daccddf

Please sign in to comment.