-
Notifications
You must be signed in to change notification settings - Fork 530
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
added solution #234
Open
sheva10barca
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
sheva10barca: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
added solution #234
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 +1,18 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
|
||
const generateRandomNumber = require('./generateRandomNumber'); | ||
const handlePlayRound = require('./handlePlayRound'); | ||
|
||
const handlePlayGame = () => { | ||
const programNumber = generateRandomNumber(); | ||
|
||
handlePlayRound(programNumber); | ||
|
||
console.log( | ||
'Welcome to the Bulls and Cows game!', | ||
'Try to guess the 4-digit number with unique digits:', | ||
); | ||
}; | ||
|
||
handlePlayGame(); |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const NUMBER_LENGTH = 4; | ||
const MAX_RANGE_VALUE = 10; | ||
|
||
function generateRandomNumber() { | ||
const usedDigits = new Set(); | ||
let result = ''; | ||
|
||
while (usedDigits.size < NUMBER_LENGTH) { | ||
const randomDigit = Math.floor(Math.random() * MAX_RANGE_VALUE); | ||
|
||
if (!usedDigits.has(randomDigit)) { | ||
usedDigits.add(randomDigit); | ||
result += randomDigit; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const getBullsAndCows = (programNumber, userNumber) => { | ||
let bulls = 0; | ||
let cows = 0; | ||
|
||
for (let i = 0; i < programNumber.length; i++) { | ||
if (userNumber[i] === programNumber[i]) { | ||
bulls++; | ||
} else if (programNumber.includes(userNumber[i])) { | ||
cows++; | ||
} | ||
} | ||
|
||
return { | ||
bulls, | ||
cows, | ||
}; | ||
}; | ||
|
||
module.exports = getBullsAndCows; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
|
||
const getBullsAndCows = require('./getBullsAndCows'); | ||
const validateInput = require('./validateInput'); | ||
const inputAndOutputOperations = require('./inputAndOutputOperations'); | ||
|
||
const handlePlayRound = (programNumber) => { | ||
inputAndOutputOperations.question('', (userNumber) => { | ||
const isInputValid = validateInput(userNumber); | ||
const result = getBullsAndCows(programNumber, userNumber); | ||
|
||
if (!isInputValid) { | ||
console.log('Please enter a valid 4-digit number with unique digits.'); | ||
handlePlayRound(programNumber); | ||
|
||
return; | ||
} | ||
|
||
if (result.bulls === 4) { | ||
console.log(`Congratulations! You guessed the ${programNumber}!`); | ||
|
||
inputAndOutputOperations.close(); | ||
} else { | ||
console.log( | ||
`The result is ${result.bulls} ${ | ||
result.bulls === 1 ? 'bull' : 'bulls' | ||
} and ${result.cows} ${ | ||
result.cows === 1 ? 'cow' : 'cows' | ||
}.\nEnter your guess: `, | ||
); | ||
|
||
handlePlayRound(programNumber); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = handlePlayRound; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
const readline = require('readline'); | ||
|
||
const inputAndOutputOperations = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
module.exports = inputAndOutputOperations; |
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,7 @@ | ||||||
'use strict'; | ||||||
|
||||||
const validateInput = (input) => { | ||||||
return /^\d{4}$/.test(input) && new Set(input).size === 4; | ||||||
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.
Suggested change
|
||||||
}; | ||||||
|
||||||
module.exports = validateInput; |
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.