-
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
add task solution #429
base: master
Are you sure you want to change the base?
add task solution #429
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Test | ||
|
||
on: | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [20.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,41 @@ | ||
'use strict'; | ||
|
||
// Write your code here | ||
const readline = require('node:readline'); | ||
const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput.js'); | ||
const { generateRandomNumber } = require('./modules/generateRandomNumber.js'); | ||
const { getBullsAndCows } = require('./modules/getBullsAndCows.js'); | ||
|
||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
const print = (message) => rl.write(message + '\n'); | ||
|
||
const secretNumber = generateRandomNumber(); | ||
|
||
// Функція, яка запускає одну спробу гри | ||
function playRound() { | ||
rl.question('Введіть ваше число: ', (guess) => { | ||
const validInput = checkIsValidUserInput(guess); | ||
|
||
if (!validInput) { | ||
print('невалідне значення'); | ||
playRound(); | ||
} | ||
|
||
if (validInput && guess !== secretNumber.toString()) { | ||
const { bulls, cows } = getBullsAndCows(guess, secretNumber); | ||
|
||
print(`Бики: ${bulls}, Корови: ${cows}`); | ||
playRound(); // Запускаємо ще один раунд | ||
} | ||
|
||
if (guess === secretNumber.toString() && validInput) { | ||
print(`Вітаємо! Ви вгадали число ${secretNumber}!`); | ||
rl.close(); | ||
} | ||
}); | ||
} | ||
|
||
playRound(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,22 @@ | |
* @return {boolean} - True if the user input is valid, false otherwise | ||
*/ | ||
function checkIsValidUserInput(userInput) { | ||
/* Write your code here */ | ||
const validInput = new Set(userInput); | ||
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. The use of Set to check for duplicate digits is incorrect here. The Set is created from the entire input string, which does not correctly check for duplicates. Consider splitting the input into individual characters and then creating a Set to check for duplicates. |
||
const firstValue = Array.from(validInput)[0]; | ||
|
||
if (validInput.size !== 4) { | ||
return false; | ||
} | ||
|
||
if (firstValue === '0') { | ||
return false; | ||
} | ||
|
||
if (isNaN(+userInput)) { | ||
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. The check for whether the input is a number should be done before checking the size of the Set to ensure the input is numeric. |
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,19 @@ | |
* @return {number} A random 4-digit number | ||
*/ | ||
function generateRandomNumber() { | ||
/* Write your code here */ | ||
const digits = new Set(); | ||
|
||
while (digits.size < 4) { | ||
const digit = Math.floor(Math.random() * 10).toString(); | ||
|
||
if (digits.size === 0 && digit === '0') { | ||
continue; | ||
} | ||
|
||
digits.add(digit); | ||
} | ||
|
||
return parseInt(Array.from(digits).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. Converting a Set to an array and then joining the digits may not preserve the order of digits as intended. Consider using a different approach to ensure the digits are ordered correctly, especially ensuring the first digit is not zero. |
||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,23 @@ | |
* Example: { bulls: 1, cows: 2 } | ||
*/ | ||
function getBullsAndCows(userInput, numberToGuess) { | ||
/* Write your code here */ | ||
const userInputStr = userInput.toString(); | ||
const numberToGuessStr = numberToGuess.toString(); | ||
|
||
const result = { | ||
bulls: 0, | ||
cows: 0, | ||
}; | ||
|
||
for (let i = 0; i < 4; i++) { | ||
if (userInputStr[i] === numberToGuessStr[i]) { | ||
result.bulls++; | ||
} else if (numberToGuessStr.includes(userInputStr[i])) { | ||
result.cows++; | ||
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. The current logic for counting cows does not account for cases where a digit in the user input has already been counted as a bull. Consider adding a mechanism to ensure that a digit is not counted as a cow if it has already been counted as a bull. |
||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
module.exports = { | ||
|
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.
Using recursion for handling invalid input can lead to a stack overflow if the user continuously inputs invalid data. Consider using a loop to handle repeated attempts instead of recursion.