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

node_bulls-and-cows #437

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
11 changes: 10 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
'use strict';

// Write your code here
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput');
const { generateRandomNumber } = require('./modules/generateRandomNumber');
const { getBullsAndCows } = require('./modules/getBullsAndCows');

const numberToGuess = generateRandomNumber();
const userInput = +process.argv.slice(2)[0];

Choose a reason for hiding this comment

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

The userInput is being converted to a number using the unary + operator. However, the checkIsValidUserInput function expects a string input. Consider removing the + to keep userInput as a string.


if (checkIsValidUserInput(userInput)) {
getBullsAndCows(userInput, numberToGuess);
}
18 changes: 17 additions & 1 deletion src/modules/checkIsValidUserInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,23 @@
* @return {boolean} - True if the user input is valid, false otherwise
*/
function checkIsValidUserInput(userInput) {
/* Write your code here */
const notNumber = isNaN(+userInput);

if (notNumber) {
return false;
}

if (userInput[0] === '0' || userInput.length !== 4) {
return false;
}

for (let i = 0; i < userInput.length; i++) {
if (userInput.slice(0, i).includes(userInput[i])) {
return false;
}
}

return true;
}

module.exports = {
Expand Down
18 changes: 17 additions & 1 deletion src/modules/generateRandomNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,23 @@
* @return {number} A random 4-digit number
*/
function generateRandomNumber() {
/* Write your code here */
const numbers = [];
const [start, end] = [1, 9];

Choose a reason for hiding this comment

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

The range [1, 9] ensures that 0 is never included in the generated number. Consider changing the range to [0, 9] and ensuring that the first digit is not 0 by swapping it with a non-zero digit if necessary.


for (let i = start; i <= end; i++) {
numbers.push(i);
}

let result = '';

for (let i = 0; i < 4; i++) {
const roundIndex = Math.floor(Math.random() * numbers.length);

result += numbers[roundIndex];
numbers.splice(roundIndex, 1);
}

return +result;
}

module.exports = {
Expand Down
18 changes: 17 additions & 1 deletion src/modules/getBullsAndCows.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@
* Example: { bulls: 1, cows: 2 }
*/
function getBullsAndCows(userInput, numberToGuess) {
/* Write your code here */
const result = {
bulls: 0,
cows: 0,
};

const userNumber = userInput.toString();
const guessNumber = numberToGuess.toString();

for (let i = 0; i < userNumber.length; i++) {
if (userNumber[i] === guessNumber[i]) {
result.bulls++;
} else if (guessNumber.includes(userNumber[i])) {
result.cows++;
}
}

return result;
}

module.exports = {
Expand Down
Loading