-
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
add task solution #423
base: master
Are you sure you want to change the base?
add task solution #423
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,22 @@ | ||
'use strict'; | ||
|
||
// Write your code here | ||
import readline from 'readline'; | ||
import { generateRandomNumber } from './modules/generateRandomNumber'; | ||
import { checkIsValidUserInput } from './modules/checkIsValidUserInput'; | ||
import { getBullsAndCows } from './modules/getBullsAndCows'; | ||
|
||
const terminal = readline.createInterface(process.stdin, process.stdout); | ||
|
||
const numberToGuess = generateRandomNumber(); | ||
|
||
terminal.question('Guess the number', (userNumber) => { | ||
if (!checkIsValidUserInput(userNumber)) { | ||
return; | ||
} | ||
|
||
if (userNumber === numberToGuess) { | ||
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 should provide feedback to the user when they guess the number correctly, such as a congratulatory message. |
||
terminal.close(); | ||
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. After closing the terminal, it might be a good idea to inform the user that they have guessed the correct number. Consider adding a message to congratulate the user. |
||
} else { | ||
getBullsAndCows(userNumber, numberToGuess); | ||
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 result of getBullsAndCows is not being used. Consider storing the result and providing feedback to the user about the number of bulls and 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 result of getBullsAndCows is not being used. You might want to display the bulls and cows count to the user. |
||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,23 @@ | |
* @return {boolean} - True if the user input is valid, false otherwise | ||
*/ | ||
function checkIsValidUserInput(userInput) { | ||
/* Write your code here */ | ||
if (userInput.length !== 4) { | ||
return false; | ||
} | ||
|
||
if (userInput[0] === '0') { | ||
return false; | ||
} | ||
|
||
const arr = [...userInput]; | ||
|
||
for (const num of arr) { | ||
if (arr.filter((number) => number === num).length > 1 || isNaN(+num)) { | ||
Comment on lines
+22
to
+23
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 checks for duplicate digits by filtering the array for each digit. This results in a time complexity of O(n^2), which is inefficient for this task. Consider using a Set to track seen digits, which will improve the efficiency of the duplicate check. 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 'isNaN(+num)' is used to check if the character is a number, but this is unnecessary since the input is expected to be a string of digits. If you want to ensure the input consists only of digits, consider using a regular expression at the beginning of the function.
Comment on lines
+22
to
+23
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 checks for duplicate digits by filtering the array for each digit and checking the length. This approach is inefficient. Consider using a Set to track seen digits, which would simplify the logic and improve performance. |
||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,17 @@ | |
* @return {number} A random 4-digit number | ||
*/ | ||
function generateRandomNumber() { | ||
/* Write your code here */ | ||
const numArr = [Math.floor(Math.random() * 9) + 1]; | ||
|
||
while (numArr.length < 4) { | ||
const newNum = Math.floor(Math.random() * 9); | ||
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 range for generating 'newNum' should be from 0 to 9, but the current implementation only generates numbers from 0 to 8. To include 9, the multiplication factor should be 10 instead of 9. 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 range for generating |
||
|
||
if (!numArr.includes(newNum)) { | ||
numArr.push(newNum); | ||
} | ||
} | ||
|
||
return +numArr.join(''); | ||
} | ||
|
||
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 score = { | ||
bulls: 0, | ||
cows: 0, | ||
}; | ||
|
||
const userArr = [...String(userInput)]; | ||
const guessArr = [...String(numberToGuess)]; | ||
|
||
for (let i = 0; i < userArr.length; i++) { | ||
if (guessArr[i] === userArr[i]) { | ||
score.bulls += 1; | ||
} else if (guessArr.includes(userArr[i])) { | ||
score.cows += 1; | ||
Comment on lines
+27
to
+28
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. There is a logical issue here. The current logic increments cows even if the digit has already been counted as a bull. You need to ensure that a digit is not counted as a cow if it has already been counted as a bull.
Comment on lines
+27
to
+28
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 is incorrect because it counts a cow even if the digit has already been counted as a bull. You should ensure that a digit is not counted as a cow if it has already been counted as a bull. |
||
} | ||
} | ||
|
||
return score; | ||
} | ||
|
||
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 program should provide feedback to the user if the input is invalid. Consider adding a message to inform the user that their input was not valid.