Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxSchmide committed Aug 7, 2023
1 parent 1355064 commit e1d64e4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { generateRandomNumber } = require('./generator.js');
const { terminal } = require('./terminal.js');
const { check } = require('./checker.js');
const { isValidGuess } = require('./guessValidation.js');

const startGame = () => {
const number = generateRandomNumber();
Expand All @@ -12,7 +13,7 @@ const startGame = () => {
terminal.question('Enter a number with 4 different digits: ', (guess) => {
const matches = check(number, guess);

if (guess.length !== 4) {
if (guess.length !== 4 || !isValidGuess(guess)) {
console.log('Not valid number!');
tryToGuess();

Expand Down
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((val, i) => arr.indexOf(val) === i);

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

module.exports = { isValidGuess };

0 comments on commit e1d64e4

Please sign in to comment.