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

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
40 changes: 39 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
/* eslint-disable no-console */
'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 rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

const numberToGuess = generateRandomNumber().toString();

function askUserForGuess() {
rl.question('Enter your guess: ', (userInput) => {
if (!checkIsValidUserInput(userInput)) {
console.log(
'Invalid input. Please enter a 4-digit number without duplicates.',
);
askUserForGuess();

return;
}

const result = getBullsAndCows(userInput, numberToGuess);

console.log(`Bulls: ${result.bulls}, Cows: ${result.cows}`);

if (result.bulls === 4) {
console.log('Congratulations! You guessed the number.');
rl.close();
} else {
askUserForGuess();
}
});
}

askUserForGuess();
29 changes: 28 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,35 @@
* @param {string} userInput - The user input
* @return {boolean} - True if the user input is valid, false otherwise
*/

function hasRepeatingdigits(numStr) {
for (let i = 0; i < numStr.length; i++) {
if (numStr.substring(i + 1).includes(numStr[i])) {
return true;
}
}

return false;
}

function checkIsValidUserInput(userInput) {
/* Write your code here */
// if (
// userInput.length !== 4 ||
// parseFloat(userInput[0]) === 0 ||
// hasRepeatingdigits(userInput)
// ) {
// return false;
// }

if (!/^\d{4}$/.test(userInput)) {
return false;
}

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

return true;
}

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

function generateDigit() {
return Math.floor(Math.random() * 10);
}

function generateRandomNumber() {
/* Write your code here */
let strNum = '';

while (strNum.length < LENGTH) {
const digit = generateDigit().toString();

if (strNum.length === 0 && digit === '0') {
continue;
}

if (!strNum.includes(digit)) {
strNum += digit;
}
}

return parseFloat(strNum);
}

module.exports = {
Expand Down
19 changes: 18 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,25 @@
* @return {object} An object containing the number of bulls and cows.
* Example: { bulls: 1, cows: 2 }
*/

function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const bullsAndCows = {
bulls: 0,
cows: 0,
};

const strToGuess = `${numberToGuess}`;
const strInput = `${userInput}`;

for (let i = 0; i < strInput.length; i++) {
if (strInput[i] === strToGuess[i]) {
bullsAndCows.bulls += 1;
} else if (strToGuess.includes(strInput[i])) {
bullsAndCows.cows += 1;
}
}

return bullsAndCows;
}

module.exports = {
Expand Down
Loading