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

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
60 changes: 60 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
'use strict';

const { getRandomDigits } = require('./generateRandom.js');
const { terminal } = require('./terminal.js');
const { check } = require('./cheacker.js');
const { isValidGuess } = require('./guessValidation.js');

const startGame = () => {
const number = getRandomDigits();

const tryToGuess = () => {
terminal.question('Enter a number with 4 different digits: ', (guess) => {
const matches = check(number, guess);

if (isNaN(guess)) {
// eslint-disable-next-line no-console
console.log('Invalid input. Please enter 4 different digits.');
tryToGuess();

return;
}

if (guess.length !== 4 || !isValidGuess(guess)) {
// eslint-disable-next-line no-console
console.log('Not valid number!');
tryToGuess();

return;
}

if (!matches.bull && !matches.cow) {
// eslint-disable-next-line no-console
console.log('Try again!');
tryToGuess();

return;
}

if (matches.bull === 4) {
// eslint-disable-next-line no-console
console.log('Congratulations!');
terminal.close();

return;
}

if (matches.bull !== 4) {
// eslint-disable-next-line no-console
console.log(`
Nice try!
You have ${matches.bull} bulls and ${matches.cow} cows
`);
tryToGuess();
}
});
};

tryToGuess();
};

startGame();
25 changes: 25 additions & 0 deletions src/cheacker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

const check = (number, guess) => {
const obj = {
bull: 0,
cow: 0,
};
const numberStr = number.toString();

guess.split('').forEach((digit, i) => {
if (numberStr[i] === digit) {
obj.bull += 1;

return;
}

if (numberStr.includes(digit)) {
obj.cow += 1;
}
});

return obj;
};

module.exports = { check };
17 changes: 17 additions & 0 deletions src/generateRandom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

function getRandomDigits() {
const digits = [];

while (digits.length < 4) {
const randomNumber = Math.floor(Math.random() * 10);

if (!digits.includes(randomNumber)) {
digits.push(randomNumber);
}
}

return digits.join('');
}

module.exports = { getRandomDigits };
11 changes: 11 additions & 0 deletions src/guessValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const isValidGuess = (guess) => {
const arr = guess.split('');

const filtered = arr.filter((value, i) => arr.indexOf(value) === i);

return arr.length === filtered.length;
};

module.exports = { isValidGuess };
10 changes: 10 additions & 0 deletions src/terminal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const readline = require('readline');

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

module.exports = { terminal };
Loading