-
Notifications
You must be signed in to change notification settings - Fork 543
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
finished #435
base: master
Are you sure you want to change the base?
finished #435
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,10 +1,11 @@ | ||
module.exports = { | ||
extends: '@mate-academy/eslint-config', | ||
env: { | ||
jest: true | ||
jest: true, | ||
}, | ||
rules: { | ||
'no-proto': 0 | ||
'no-proto': 0, | ||
'prettier/prettier': ['error', { endOfLine: 'lf' }], | ||
}, | ||
plugins: ['jest'] | ||
plugins: ['jest'], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,21 @@ name: Test | |
|
||
on: | ||
pull_request: | ||
branches: [ master ] | ||
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 | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
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. Consider using 'actions/setup-node@v3' instead of 'v1' to ensure you are using the latest features and security updates. 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. Consider updating the 'setup-node' action to 'v3' to benefit from the latest features and security updates. |
||
with: | ||
node-version: ${{ matrix.node-version }} | ||
- run: npm install | ||
- run: npm test |
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,35 @@ | ||
'use strict'; | ||
import readline from 'node:readline'; | ||
import { generateRandomNumber } from './modules/generateRandomNumber.js'; | ||
import { checkIsValidUserInput } from './modules/checkIsValidUserInput.js'; | ||
import { getBullsAndCows } from './modules/getBullsAndCows.js'; | ||
|
||
// Write your code here | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
const hiddenNumber = generateRandomNumber(); | ||
|
||
const requestANumber = () => { | ||
rl.question('Write your number? ', (userValue) => { | ||
const isValidUserInput = checkIsValidUserInput(userValue); | ||
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. Ensure that the |
||
|
||
if (!isValidUserInput) { | ||
requestANumber(); | ||
|
||
return; | ||
} | ||
|
||
const bullsAndCows = getBullsAndCows(userValue, hiddenNumber); | ||
|
||
if (bullsAndCows.bulls === 4) { | ||
rl.close(); | ||
|
||
return; | ||
} | ||
|
||
requestANumber(); | ||
}); | ||
}; | ||
|
||
requestANumber(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,48 @@ | |
* @param {string} userInput - The user input | ||
* @return {boolean} - True if the user input is valid, false otherwise | ||
*/ | ||
const checkIsUniqueNumbers = (value) => { | ||
const arrayValue = `${value}`.split(''); | ||
|
||
if (arrayValue.length !== 4 && typeof value !== 'number') { | ||
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 condition here should use '||' instead of '&&'. It should check if the length is not 4 or if the type is not a number. |
||
return false; | ||
} | ||
|
||
if (arrayValue[0] === '0') { | ||
return false; | ||
} | ||
|
||
for (let i = 0; i < arrayValue.length; i++) { | ||
const numberToCheck = arrayValue[i]; | ||
|
||
arrayValue.splice(i, 1); | ||
|
||
if (arrayValue.includes(numberToCheck)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
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 |
||
} | ||
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 loop in |
||
}; | ||
|
||
function checkIsValidUserInput(userInput) { | ||
/* Write your code here */ | ||
|
||
const numberedUserInput = +userInput; | ||
|
||
if (userInput.length !== 4) { | ||
return false; | ||
} | ||
|
||
if (typeof numberedUserInput !== 'number' || isNaN(numberedUserInput)) { | ||
return false; | ||
} | ||
|
||
if (+userInput[0] === 0) { | ||
return false; | ||
} | ||
|
||
return checkIsUniqueNumbers(userInput); | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,30 @@ | |
*/ | ||
function getBullsAndCows(userInput, numberToGuess) { | ||
/* Write your code here */ | ||
const userInputArr = userInput.toString().split(''); | ||
const numberToGuessArr = numberToGuess.toString().split(''); | ||
const result = { bulls: 0, cows: 0 }; | ||
|
||
for (let i = 0; i < userInputArr.length; i++) { | ||
if ( | ||
numberToGuessArr.includes(userInputArr[i]) && | ||
numberToGuessArr.indexOf(userInputArr[i]) === i | ||
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 condition for checking bulls is incorrect. It should check if the digits are the same and in the same position, but the current logic checks if the digit exists in the array and is in the same position, which is redundant. Consider simplifying this condition to directly compare the digits at the same index. |
||
) { | ||
result.bulls += 1; | ||
} else if (numberToGuessArr.includes(userInputArr[i])) { | ||
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 condition for checking cows should ensure that the digit is not already counted as a bull. Consider adding a check to ensure the digit is not in the correct position before counting it as a cow. |
||
result.cows += 1; | ||
} | ||
} | ||
|
||
// if (result.bulls === 4) { | ||
// console.log('you won!'); | ||
|
||
// return; | ||
// } | ||
|
||
// console.log(result); | ||
|
||
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 'no-proto' rule is set to 0, which disables it. If this is not intentional, consider setting it to 'warn' or 'error' to enforce the rule.