-
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 all commits
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 (!checkIsValidUserInput(guess)) { | ||
print('невалідне значення'); | ||
playRound(); | ||
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. 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. |
||
} | ||
|
||
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 |
---|---|---|
|
@@ -13,7 +13,35 @@ | |
* 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, | ||
}; | ||
|
||
const checkedPositions = new Array(4).fill(false); | ||
|
||
for (let i = 0; i < 4; i++) { | ||
if (userInputStr[i] === numberToGuessStr[i]) { | ||
result.bulls++; | ||
checkedPositions[i] = true; | ||
} | ||
} | ||
|
||
for (let i = 0; i < 4; i++) { | ||
if (userInputStr[i] !== numberToGuessStr[i]) { | ||
const cowIndex = numberToGuessStr.indexOf(userInputStr[i]); | ||
|
||
if (cowIndex !== -1 && !checkedPositions[cowIndex]) { | ||
result.cows++; | ||
checkedPositions[cowIndex] = true; | ||
Comment on lines
+35
to
+39
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 logic for calculating cows is incorrect. The current implementation checks for the presence of a digit in the |
||
} | ||
} | ||
} | ||
|
||
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.
The variable
validInput
is assigned the result ofcheckIsValidUserInput(guess)
, but the same function is called again in the if condition on line 22. You can usevalidInput
directly in the condition to avoid redundant function calls.