Skip to content

Commit

Permalink
add condition isNumber for validation, refactor generateNumber function
Browse files Browse the repository at this point in the history
  • Loading branch information
kaavka committed Oct 10, 2023
1 parent 49b8e53 commit b4206b2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/generateNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ function generateNumber() {
const distinctDigits = [];

while (distinctDigits.length < EXPECTED_NUMBER_LENGTH) {
const randomIndex = Math.floor(Math.random() * digitsToInsert.length);
let randomIndex = 0;

if (!distinctDigits.length && digitsToInsert[randomIndex] === 0) {
continue;
if (!distinctDigits.length) {
randomIndex = Math.floor(Math.random() * (digitsToInsert.length - 1) + 1);
} else {
randomIndex = Math.floor(Math.random() * digitsToInsert.length);
}

const digitToInsert = digitsToInsert.splice(randomIndex, 1);
const digitToInsert = digitsToInsert.splice(randomIndex, 1)[0];

distinctDigits.push(digitToInsert);
}
Expand Down
3 changes: 2 additions & 1 deletion src/validateInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const { EXPECTED_NUMBER_LENGTH } = require('./constants');
function validateInput(input) {
const inputSplited = input.split('');
const inputSet = new Set(inputSplited);
const isNumber = inputSplited.every(num => num >= 0 && num <= 9);
const hasValidLength = inputSplited.length === EXPECTED_NUMBER_LENGTH;
const hasDistinctDigits = inputSplited.length === inputSet.size;

return hasValidLength && hasDistinctDigits;
return hasValidLength && hasDistinctDigits && isNumber;
};

module.exports = {
Expand Down

0 comments on commit b4206b2

Please sign in to comment.