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

Solution #433

Open
wants to merge 3 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('node:readline');
const { stdin, stdout, exit } = require('node:process');

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

const PROMPT_MESSAGE = 'Write 4 digits: \n';
const INVALID_INPUT_MESSAGE = 'The input must be 4 uniq digits: \n';
const TRY_AGAIN_MESSAGE = 'Need to try one more time: \n';
const WIN_MESSAGE = 'You win! The correct answer is';

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

stdout.write(PROMPT_MESSAGE);

input.on('line', (text) => {
if (!checkIsValidUserInput(text)) {
stdout.write(INVALID_INPUT_MESSAGE);
}

Choose a reason for hiding this comment

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

Currently, if the input is invalid, the program only writes an error message but does not return or prompt the user to input again. You should add a return statement after writing the INVALID_INPUT_MESSAGE to prevent further execution of the code block and prompt the user to try again.


const { bulls, cows } = getBullsAndCows(text, answer);

if (bulls === 4) {
stdout.write(`${WIN_MESSAGE} ${text}`);
input.close();
exit();
}

stdout.write(`{bulls: ${bulls}, cows: ${cows}} \n${TRY_AGAIN_MESSAGE}`);
});
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 */
switch (true) {
case userInput[0] === '0':
return false;

case isNaN(+userInput):
return false;

case new Set(userInput.split('')).size !== 4:
return false;

default:
return true;
}
}

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

function generateRandomNumber() {
/* Write your code here */
const uniqRandomNumbers = new Set();

while (uniqRandomNumbers.size !== 4) {
const randomNumber = Math.floor(1 + Math.random() * 9);

Choose a reason for hiding this comment

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

The current logic for generating random numbers only produces digits from 1 to 9. This ensures no leading zero, but the logic for ensuring exactly 4 unique digits is flawed. Consider modifying the logic to ensure the first digit is non-zero and the remaining digits are unique, potentially by generating the first digit separately and then filling the rest.


uniqRandomNumbers.add(randomNumber);
}

return Number(Array.from(uniqRandomNumbers).join(''));

Choose a reason for hiding this comment

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

The conversion of the set to a number may result in fewer than 4 digits if the set contains leading zeros. Ensure that the number is always 4 digits long by adjusting the logic to handle leading zeros appropriately.

}

module.exports = {
Expand Down
22 changes: 21 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,27 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
let bulls = 0;
let cows = 0;

const userInputStr = String(userInput);
const numberToGuessStr = String(numberToGuess);

for (let i = 0; i < userInputStr.length; i++) {
const guessNumber = userInputStr[i];
const rightNumber = numberToGuessStr[i];

if (guessNumber === rightNumber) {
bulls += 1;
} else if (
numberToGuessStr.includes(guessNumber) &&
guessNumber !== rightNumber
) {
cows += 1;
}
}

return { bulls, cows };
}

module.exports = {
Expand Down