From e157b559ffa90df2de4cee7dbd5bde27212f0807 Mon Sep 17 00:00:00 2001 From: an-marryKyslenko Date: Wed, 1 Jan 2025 11:33:38 +0100 Subject: [PATCH] add solution --- src/app.js | 27 ++++++++++++++++++++++++++- src/modules/checkIsValidUserInput.js | 8 ++++++++ src/modules/generateRandomNumber.js | 11 +++++++++++ src/modules/getBullsAndCows.js | 16 ++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index e89a2d97..6338f730 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,28 @@ 'use strict'; -// Write your code here +/* eslint-disable no-console */ +const readline = require('node:readline'); +const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput'); +const { getBullsAndCows } = require('./modules/getBullsAndCows'); +const { generateRandomNumber } = require('./modules/generateRandomNumber'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +rl.question('Put you number: ', (userInput) => { + console.log(checkIsValidUserInput(userInput)); + + if (checkIsValidUserInput(userInput)) { + const numberToGuess = generateRandomNumber(); + const result = getBullsAndCows(userInput, numberToGuess); + + console.log(`Your input: ${+userInput}`); + console.log(`Number to guess: ${+numberToGuess}`); + console.log(`Result: ${JSON.stringify(result)}`); + } else { + console.log('Invalid input. Please enter a valid number.'); + } + rl.close(); +}); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..89c548cc 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -10,6 +10,14 @@ */ function checkIsValidUserInput(userInput) { /* Write your code here */ + const testInput = new Set(userInput); + const regex = /^[1-9]\d{3}$/; + + if (testInput.size !== userInput.length || !regex.test(userInput)) { + return false; + } + + return true; } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..66a32f95 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -8,6 +8,17 @@ */ function generateRandomNumber() { /* Write your code here */ + let numbers = ''; + + while (numbers.length < 4) { + const number = Math.floor(Math.random() * 9) + 1; + + if (!numbers.includes(number)) { + numbers += number; + } + } + + return Number(numbers); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..7158941e 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -14,6 +14,22 @@ */ function getBullsAndCows(userInput, numberToGuess) { /* Write your code here */ + const result = { + bulls: 0, + cows: 0, + }; + const userInputStr = userInput.toString(); + const numberToGuessStr = numberToGuess.toString(); + + for (let i = 0; i < 4; i++) { + if (userInputStr[i] === numberToGuessStr[i]) { + result.bulls += 1; + } else if (numberToGuessStr.includes(userInputStr[i])) { + result.cows += 1; + } + } + + return result; } module.exports = {