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

Develop #428

Open
wants to merge 2 commits 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
41 changes: 40 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
'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();
let isFirstAttempt = true;
const print = (message) => terminal.write(message + '\n');

const askQuestion = () => {
const prompt = isFirstAttempt
? `I generated a number of 4 different digits. Please, guess a number :)\n`
: 'Your guess:\n';

terminal.question(prompt, (userInput) => {
isFirstAttempt = false;

if (!checkIsValidUserInput(userInput)) {
print('Your input is invalid, please, try again :)');

return askQuestion();
}

const { bulls, cows } = getBullsAndCows(userInput, numberToGuess);

print(`bulls: ${bulls}, cows: ${cows}\n`);

if (bulls === 4) {
print('Congratulations!');

return terminal.close();
}

askQuestion();
});
};

askQuestion();
26 changes: 25 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,31 @@
* @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.length !== 4) {
return false;
}

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

for (let i = 0; i < userInput.length; i++) {
for (let j = 0; j < userInput.length; j++) {
if (i === j) {
continue;
}

if (userInput[i] === userInput[j]) {
return false;
}
}
Comment on lines +24 to +33

Choose a reason for hiding this comment

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

The nested loop used to check for duplicate digits can be optimized. Currently, it checks each pair of digits twice, which is unnecessary. Consider using a Set to track seen digits, which can reduce the complexity and improve performance.

}

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 */
let resultStr = '';

while (resultStr.length < 4) {
const randomDigitStr = String(Math.floor(Math.random() * 10));

if (resultStr.length === 0) {
if (randomDigitStr !== '0') {
resultStr += randomDigitStr;
}
} else if (!resultStr.includes(randomDigitStr)) {
resultStr += randomDigitStr;
}
}

return +resultStr;

Choose a reason for hiding this comment

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

The function returns a number by converting the string resultStr to a number using the unary plus operator. This is correct as per the function's documentation, which specifies that it should return a number. Ensure that this conversion is intentional and aligns with the rest of your application's logic.

}

module.exports = {
Expand Down
20 changes: 19 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const numberToGuessStr = String(numberToGuess);
const userInputStr = String(userInput);
const numberToGuessStrArr = numberToGuessStr.split('');
const userInputStrArr = userInputStr.split('');

const result = {
bulls: 0,
cows: 0,
};

for (let i = 0; i < userInputStrArr.length; i++) {
if (userInputStrArr[i] === numberToGuessStrArr[i]) {
result.bulls++;
} else if (numberToGuessStr.includes(userInputStrArr[i])) {
result.cows++;
Comment on lines +29 to +30

Choose a reason for hiding this comment

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

The current logic for counting cows might incorrectly count a digit as a cow if it appears multiple times in the user input, even if it has already been counted as a bull. Consider adding a mechanism to track which digits have already been matched to avoid counting them multiple times.

}
}

return result;
}

module.exports = {
Expand Down
Loading