-
Notifications
You must be signed in to change notification settings - Fork 538
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
feat: add terminal game functionality #308
base: master
Are you sure you want to change the base?
Conversation
const uniqueNumbers = new Set(userInput); | ||
|
||
if (userInput.length !== 4) { | ||
return false; | ||
} | ||
|
||
if (isNaN(+userInput)) { | ||
return false; | ||
} | ||
|
||
if (+userInput < 1000) { | ||
return false; | ||
} | ||
|
||
if (uniqueNumbers.size !== userInput.length) { | ||
return false; | ||
} | ||
|
||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
U can write this in one line
function checkIsValidUserInput(userInput) {
return userInput.length === 4
&& !isNaN(+userInput)
&& +userInput >= 1000
&& new Set(userInput).size === 4;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
U can write this in one line
function checkIsValidUserInput(userInput) { return userInput.length === 4 && !isNaN(+userInput) && +userInput >= 1000 && new Set(userInput).size === 4; }
Yes, I can do this, but it seems to me that the option with if is more readable, so it is more optimal from a calculating point of view that you do not have to check all the conditions.
Thank you for your comment 👍 . I would like to know what the mentor thinks about this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is purely personal and has no performance impact, so whatever (and your team) is up to.
In the professional environment you have to comply to the code styling approved together with your team.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix your code style
const uniqueNumbers = new Set(userInput); | ||
|
||
if (userInput.length !== 4) { | ||
return false; | ||
} | ||
|
||
if (isNaN(+userInput)) { | ||
return false; | ||
} | ||
|
||
if (+userInput < 1000) { | ||
return false; | ||
} | ||
|
||
if (uniqueNumbers.size !== userInput.length) { | ||
return false; | ||
} | ||
|
||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is purely personal and has no performance impact, so whatever (and your team) is up to.
In the professional environment you have to comply to the code styling approved together with your team.
src/modules/getGame.js
Outdated
terminal.question( | ||
'Guess the number of 4 different digits, starts from 1: ', | ||
(userInput) => { | ||
if (!checkIsValidUserInput(userInput)) { | ||
console.log('(◕ ▃ ◕) Please enter 4 unique digits, starts from 1!'); | ||
getGame(randomNumber); | ||
} else { | ||
const { bulls, cows } = getBullsAndCows(userInput, randomNumber); | ||
|
||
if (bulls === 4) { | ||
console.log('(=^・^=) You won! This is the correct number!'); | ||
|
||
terminal.question('Do yoy wanna play again? Y/N?: ', (answer) => { | ||
if (answer.toLocaleLowerCase() === 'y') { | ||
newGame(); | ||
} else { | ||
console.log('(◕ ᴥ ◕) Bye!'); | ||
terminal.close(); | ||
} | ||
}); | ||
} else if (bulls || cows) { | ||
console.log( | ||
`(◔ ╭╮ ◔) Try again, You found ${bulls} bulls in the right place and ${cows} cows` | ||
); | ||
getGame(randomNumber); | ||
} else { | ||
console.log(`(◔ ╭╮ ◔) Try again, You didn't find anything for this shot`); | ||
getGame(randomNumber); | ||
} | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid nesting if statements, you can use guards like here https://www.codingbeautydev.com/blog/stop-using-nested-ifs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
(◕ ᴥ ◕) Guess the number of 4 different digits game