-
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
Develop #441
Open
DariaNastas
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
DariaNastas:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Develop #441
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
|
||
// Write your code here | ||
const readline = require('node:readline'); | ||
const { generateRandomNumber } = require('./generateRandomNumber'); | ||
const { checkIsValidUserInput } = require('./checkIsValidUserInput'); | ||
const { getBullsAndCows } = require('./getBullsAndCows'); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const secretNumber = generateRandomNumber(); | ||
|
||
function promptUserForGuess() { | ||
return new Promise((resolve) => { | ||
rl.question('Enter your 4-digit number: ', (userInput) => { | ||
resolve(userInput); | ||
}); | ||
}); | ||
} | ||
|
||
async function startGame() { | ||
try { | ||
const userInput = await promptUserForGuess(); | ||
|
||
if (checkIsValidUserInput(userInput)) { | ||
const result = getBullsAndCows(userInput, secretNumber); | ||
|
||
console.log(result); | ||
} else { | ||
console.log( | ||
// eslint-disable-next-line max-len | ||
'Invalid number! Please enter a valid 4-digit number with unique digits.', | ||
); | ||
} | ||
} catch (error) { | ||
console.error('An error occurred:', error.message); | ||
} finally { | ||
rl.close(); | ||
} | ||
} | ||
|
||
startGame(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Checks that the user input is valid. | ||
* Valid user input is a 4-digit number that does not start with 0 | ||
* and does not contain any duplicate digits. | ||
* | ||
* @param {string} userInput - The user input | ||
* @return {boolean} - True if the user input is valid, false otherwise | ||
*/ | ||
function checkIsValidUserInput(userInput) { | ||
/* Write your code here */ | ||
if ( | ||
userInput.length !== 4 || | ||
userInput[0] === '0' || | ||
!/^\d{4}$/.test(userInput) || | ||
new Set(userInput).size !== 4 | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
checkIsValidUserInput, | ||
}; | ||
module.exports = { checkIsValidUserInput }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Generate a random 4-digit number that does not start with 0 | ||
* and does not contain any duplicate digits. | ||
* | ||
* @return {number} A random 4-digit number | ||
*/ | ||
/* eslint-disable no-console */ | ||
/* eslint-disable padding-line-between-statements */ | ||
function generateRandomNumber() { | ||
/* Write your code here */ | ||
let randomNumber; | ||
|
||
do { | ||
randomNumber = Math.floor(Math.random() * 9000) + 1000; | ||
} while (new Set(randomNumber.toString()).size !== 4); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log('Generated Random Number:', randomNumber); | ||
|
||
return randomNumber; | ||
} | ||
|
||
module.exports = { | ||
generateRandomNumber, | ||
}; | ||
const newRandomNumber = generateRandomNumber(); | ||
console.log('Random Number from Function:', newRandomNumber); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This console log statement is also for debugging. Ensure it's necessary for your application, or remove it if it's not needed. |
||
module.exports = { generateRandomNumber }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
'use strict'; | ||
function getBullsAndCows(userInput, targetNumber) { | ||
const userInputStr = userInput.toString(); | ||
const targetNumberStr = targetNumber.toString(); | ||
const result = { bulls: 0, cows: 0 }; | ||
const targetNumberDigits = Array.from(targetNumberStr); | ||
|
||
/** | ||
* Calculate the number of bulls and cows for a given user input. | ||
* Bulls are digits that are in the correct position. | ||
* Cows are digits that are in the wrong position. | ||
* Assume that the user input and the number to guess | ||
* are always 4-digit numbers. | ||
* | ||
* @param {number} userInput - The user input | ||
* @param {number} numberToGuess - The number to guess | ||
* @return {object} An object containing the number of bulls and cows. | ||
* Example: { bulls: 1, cows: 2 } | ||
*/ | ||
function getBullsAndCows(userInput, numberToGuess) { | ||
/* Write your code here */ | ||
for (let i = 0; i < targetNumberStr.length; i++) { | ||
if (userInputStr[i] === targetNumberStr[i]) { | ||
result.bulls += 1; | ||
targetNumberDigits[i] = null; | ||
} | ||
} | ||
|
||
for (let i = 0; i < targetNumberStr.length; i++) { | ||
if (userInputStr[i] !== targetNumberStr[i]) { | ||
const indexInGuess = targetNumberDigits.indexOf(userInputStr[i]); | ||
|
||
if (indexInGuess !== -1) { | ||
result.cows += 1; | ||
targetNumberDigits[indexInGuess] = null; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
module.exports = { | ||
getBullsAndCows, | ||
}; | ||
module.exports = { getBullsAndCows }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The console log statement here is used for debugging purposes. If this is not needed in the final version, consider removing or commenting it out to avoid unnecessary console output.