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 #310

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
48 changes: 47 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
'use strict';

// Write your code here
const readline = require('readline');
const { generateRandomNumber } = require('./modules/generateRandomNumber');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { getBullsAndCows } = require('./modules/getBullsAndCows');

const secretNumber = generateRandomNumber();

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

function playGame() {
terminal.question('Guess the number: ', (userInput) => {
const userNumber = +userInput;

if (checkIsValidUserInput(userNumber)) {
const result = getBullsAndCows(
userInput.toString(), secretNumber.toString()
);

// eslint-disable-next-line no-console
console.log(`Bulls: ${result.bulls}, Cows: ${result.cows}`);

if (result.bulls === 4) {
// eslint-disable-next-line no-console
console.log('Congratulations! You guessed the correct number.');
terminal.close();
} else {
playGame();
}
} else {
// eslint-disable-next-line no-console
console.log(
'Invalid input. Please enter a 4-digit number with no duplicate digits.'
);
playGame();
}
});
}

// eslint-disable-next-line no-console
console.log(
'Welcome to the Bulls and Cows game! Try to guess the 4-digit number.'
);

playGame();
13 changes: 12 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const inputLength = userInput.length;
const numericInput = userInput;
const isNumeric = !isNaN(numericInput);
const isInRange = numericInput >= 1000;
const uniqueDigits = new Set(userInput);

const isValid = uniqueDigits.size === inputLength
&& isNumeric
&& isInRange
&& inputLength === 4;

return isValid;
}

module.exports = {
Expand Down
18 changes: 17 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const number = [];

while (number.length < 4) {
let randomNumber = 0;

if (number.length === 0) {
randomNumber = Math.floor(Math.random() * 8) + 1;
} else {
do {
randomNumber = Math.floor(Math.random() * 9);
} while (number.includes(randomNumber));
}

number.push(randomNumber);
}

return Number([...number].join(''));
}

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

for (let i = 0; i < 4; i++) {
if (input[i] === number[i]) {
bullsAndCows.bulls += 1;
} else if (number.includes(input[i])) {
bullsAndCows.cows += 1;
}
}

return bullsAndCows;
}

module.exports = {
Expand Down
Loading