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

feat: add terminal game functionality #308

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kate-prakofyeva
Copy link

(◕ ᴥ ◕) Guess the number of 4 different digits game

Comment on lines +13 to +31
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;

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;
}

Copy link
Author

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

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.

Copy link

@mbruhler mbruhler left a 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

Comment on lines +13 to +31
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;

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.

Comment on lines 17 to 47
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);
}
}
});

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants