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 task solution #400

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
59 changes: 58 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
/* eslint-disable no-console */
'use strict';

// Write your code here
const { generateRandomNumber } = require('./modules/generateRandomNumber');
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { getBullsAndCows } = require('./modules/getBullsAndCows');
const numberToGuess = generateRandomNumber();
const errorMessage = 'Please ender correct number! ';
const getQuestion = (prevAnswer) =>
prevAnswer === errorMessage
? errorMessage
: 'Try to guess 4-digit number again! ';

const getFinalMessage = (answer) => {
let message;

if (!checkIsValidUserInput(answer)) {
message = errorMessage;
} else {
const { bulls, cows } = getBullsAndCows(+answer.trim(), numberToGuess);

message = `Your results are: bulls: ${bulls}, cows: ${cows}`;
}

return message;
};

const logMessage = (message) =>
message !== errorMessage ? console.log(message) : '';

const readline = require('node:readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.question(`Try to guess 4-digit number `, (answer1) => {
const message1 = getFinalMessage(answer1);

logMessage(message1);

if (message1 !== 'You won! Congratulations!') {
rl.question(getQuestion(message1), (answer2) => {
const message2 = getFinalMessage(answer2);

logMessage(message2);

if (message2 !== 'You won! Congratulations!') {
rl.question(getQuestion(message2), (answer3) => {
const message3 = getFinalMessage(answer3);

if (message3 !== 'You won! Congratulations!') {
console.log('You lost!');
}
rl.close();
});
}
});
}
});
16 changes: 15 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const input = new Set(userInput.split(''));
const startsWith0 = userInput.trim()[0] === '0';
const isLongEnough = input.size !== 4;

if (startsWith0 || isLongEnough) {
return false;
}

for (const num of input.values()) {
if (isNaN(+num)) {
return false;
}
}

return true;
}

module.exports = {
Expand Down
10 changes: 9 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const getNumber = () => Math.random().toString().slice(2, 6).split('');

let set = new Set(getNumber());

while (set.size < 4 || set.has('0')) {
set = new Set(getNumber());
}

return +[...set.values()].join('');
}

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

for (let i = 0; i < normNumberToGuess.length; i++) {
if (normUserInput[i] === normNumberToGuess[i]) {
initialScore.bulls = initialScore.bulls + 1;
} else if (normNumberToGuess.includes(normUserInput[i])) {
initialScore.cows = initialScore.cows + 1;
}
}

return initialScore;
}

module.exports = {
Expand Down
Loading