-
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
The task is completed #233
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,33 @@ | ||
/* eslint-disable no-console */ | ||
'use strict'; | ||
|
||
const { getBullsAndCows } = require('./bullsAndCows'); | ||
const { generateNumber } = require('./generateNumber'); | ||
const { terminal } = require('./terminal'); | ||
const { isNumberValid } = require('./validateNumber'); | ||
|
||
const generatedNumber = String(generateNumber()); | ||
|
||
const startGame = () => { | ||
terminal.question('Try to quess the number:\n', (enteredNumber) => { | ||
if (!isNumberValid(enteredNumber)) { | ||
console.log('Invalid number\n'); | ||
startGame(); | ||
} else { | ||
const bullsAndCows = getBullsAndCows(generatedNumber, enteredNumber); | ||
const bulls = bullsAndCows[0]; | ||
const cows = bullsAndCows[1]; | ||
|
||
console.log(`Bulls: ${bulls}, Cows: ${cows}\n`); | ||
|
||
if (bulls === 4) { | ||
console.log('Congratulations, you win!'); | ||
terminal.close(); | ||
} else { | ||
startGame(); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
startGame(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
const NUM_LENGTH = 4; | ||
|
||
module.exports.getBullsAndCows = (numberToGuess, enteredNumber) => { | ||
const numberToGuessCopy = numberToGuess.split(''); | ||
const enteredNumberCopy = enteredNumber.split(''); | ||
let bullsCount = 0; | ||
let cowsCount = 0; | ||
|
||
for (let i = 0; i < NUM_LENGTH; i++) { | ||
const isBull = enteredNumberCopy[0] === numberToGuessCopy[0]; | ||
const isCow = numberToGuess.includes(enteredNumberCopy[0]); | ||
|
||
if (isBull) { | ||
bullsCount++; | ||
} else if (isCow) { | ||
cowsCount++; | ||
} | ||
|
||
numberToGuessCopy.shift(); | ||
enteredNumberCopy.shift(); | ||
} | ||
|
||
return [bullsCount, cowsCount]; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const NUM_LENGTH = 4; | ||
|
||
module.exports.generateNumber = () => { | ||
const digitsArray = []; | ||
|
||
while (digitsArray.length < NUM_LENGTH) { | ||
const numToPush = Math.floor(Math.random() * 10); | ||
|
||
if (!digitsArray.includes(numToPush)) { | ||
digitsArray.push(numToPush); | ||
} | ||
} | ||
|
||
return digitsArray.join(''); | ||
}; | ||
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. you func cant create number with duplicate number and can create number that start from 0, that not so logic |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
const readline = require('readline'); | ||
|
||
const terminal = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
module.exports = { terminal }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const NUM_LENGTH = 4; | ||
|
||
module.exports.isNumberValid = (number) => { | ||
for (let i = 1; i < NUM_LENGTH; i++) { | ||
if (number[i] === number[i - 1]) { | ||
return false; | ||
} | ||
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 redundant but why do you think that the same number will be placed only together |
||
} | ||
|
||
return !isNaN(+number) && number.length === NUM_LENGTH; | ||
}; |
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.