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

add solution #442

Open
wants to merge 1 commit 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
27 changes: 26 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -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}`);
Comment on lines +21 to +22

Choose a reason for hiding this comment

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

The unary + operator is used here to convert userInput and numberToGuess to numbers, but they are already numbers. This conversion is unnecessary and can be removed.

console.log(`Result: ${JSON.stringify(result)}`);
} else {
console.log('Invalid input. Please enter a valid number.');
}
rl.close();

Choose a reason for hiding this comment

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

Closing the readline interface here ends the game after the first guess. To allow the user to make multiple guesses until they find the correct number, consider restructuring the code to keep the interface open until the correct number is guessed.

});
8 changes: 8 additions & 0 deletions src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
11 changes: 11 additions & 0 deletions src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
16 changes: 16 additions & 0 deletions src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Loading