diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 00000000..bb13dfc4 --- /dev/null +++ b/.github/workflows/test.yml-template @@ -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 diff --git a/package-lock.json b/package-lock.json index 185b2268..de1b0c7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,12 @@ "version": "1.0.0", "hasInstallScript": true, "license": "GPL-3.0", + "dependencies": { + "readline": "^1.3.0" + }, "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^1.9.12", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -1467,10 +1470,11 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "1.9.12", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.9.12.tgz", + "integrity": "sha512-/OcmxMa34lYLFlGx7Ig926W1U1qjrnXbjFJ2TzUcDaLmED+A5se652NcWwGOidXRuMAOYLPU2jNYBEkKyXrFJA==", "dev": true, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", @@ -7498,6 +7502,12 @@ "integrity": "sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==", "dev": true }, + "node_modules/readline": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/readline/-/readline-1.3.0.tgz", + "integrity": "sha512-k2d6ACCkiNYz222Fs/iNze30rRJ1iIicW7JuX/7/cozvih6YCkFZH+J6mAFDVgv0dRBaAyr4jDqC95R2y4IADg==", + "license": "BSD" + }, "node_modules/regexp.prototype.flags": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/regexp.prototype.flags/-/regexp.prototype.flags-1.5.2.tgz", diff --git a/package.json b/package.json index 216da526..919085a9 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "license": "GPL-3.0", "devDependencies": { "@mate-academy/eslint-config": "latest", - "@mate-academy/scripts": "^1.8.6", + "@mate-academy/scripts": "^1.9.12", "eslint": "^8.57.0", "eslint-plugin-jest": "^28.6.0", "eslint-plugin-node": "^11.1.0", @@ -27,5 +27,8 @@ }, "mateAcademy": { "projectType": "javascript" + }, + "dependencies": { + "readline": "^1.3.0" } } diff --git a/src/app.js b/src/app.js index e89a2d97..16f614f2 100644 --- a/src/app.js +++ b/src/app.js @@ -1,3 +1,37 @@ +/* eslint-disable no-console */ 'use strict'; -// Write your code here +const readline = require('node:readline'); +const { getBullsAndCows } = require('./modules/getBullsAndCows'); +const { generateRandomNumber } = require('./modules/generateRandomNumber'); +const { checkIsValidUserInput } = require('./modules/checkIsValidUserInput'); + +const terminal = readline.createInterface(process.stdin, process.stdout); +const randomNumber = generateRandomNumber(); + +const askQuestion = () => { + terminal.question(`Please, enter a number `, (num) => { + const userNumber = checkIsValidUserInput(num); + + if (!userNumber) { + console.log('Invalid input. '); + } else { + const bullsAndCows = getBullsAndCows(Number(num), randomNumber); + + if (bullsAndCows.bulls === 4) { + console.log('Cngrats you are winner'); + terminal.close(); + + return; + } + + console.log( + `Bulls: ${bullsAndCows.bulls}, Cows: ${bullsAndCows.cows}. Try again.`, + ); + } + + askQuestion(); + }); +}; + +askQuestion(); diff --git a/src/modules/checkIsValidUserInput.js b/src/modules/checkIsValidUserInput.js index 40979664..5587d0fc 100644 --- a/src/modules/checkIsValidUserInput.js +++ b/src/modules/checkIsValidUserInput.js @@ -1,5 +1,7 @@ 'use strict'; +const { passRules } = require('../utils/passRules'); + /** * Checks that the user input is valid. * Valid user input is a 4-digit number that does not start with 0 @@ -9,7 +11,7 @@ * @return {boolean} - True if the user input is valid, false otherwise */ function checkIsValidUserInput(userInput) { - /* Write your code here */ + return passRules(userInput); } module.exports = { diff --git a/src/modules/generateRandomNumber.js b/src/modules/generateRandomNumber.js index 14ad1e2b..666df1c4 100644 --- a/src/modules/generateRandomNumber.js +++ b/src/modules/generateRandomNumber.js @@ -1,5 +1,7 @@ 'use strict'; +const { passRules } = require('../utils/passRules'); + /** * Generate a random 4-digit number that does not start with 0 * and does not contain any duplicate digits. @@ -7,7 +9,13 @@ * @return {number} A random 4-digit number */ function generateRandomNumber() { - /* Write your code here */ + const number = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000; + + if (passRules(number)) { + return number; + } + + return generateRandomNumber(); } module.exports = { diff --git a/src/modules/getBullsAndCows.js b/src/modules/getBullsAndCows.js index 3f0b39a6..783bc677 100644 --- a/src/modules/getBullsAndCows.js +++ b/src/modules/getBullsAndCows.js @@ -13,7 +13,26 @@ * Example: { bulls: 1, cows: 2 } */ function getBullsAndCows(userInput, numberToGuess) { - /* Write your code here */ + const result = { bulls: 0, cows: 0 }; + const userInputStr = String(userInput); + const numberToGuessStr = String(numberToGuess); + + if (userInputStr.length !== 4) { + return result; + } + + for (let i = 0; i < 4; i++) { + if (userInputStr[i] === numberToGuessStr[i]) { + result.bulls++; + continue; + } + + if (numberToGuessStr.includes(userInputStr[i])) { + result.cows++; + } + } + + return result; } module.exports = { diff --git a/src/utils/hasUniqDigits.js b/src/utils/hasUniqDigits.js new file mode 100644 index 00000000..b8b0ad62 --- /dev/null +++ b/src/utils/hasUniqDigits.js @@ -0,0 +1,8 @@ +function hasUniqDigits(num) { + const numStr = String(num); + const uniqDigits = new Set(numStr); + + return uniqDigits.size === numStr.length; +} + +module.exports = { hasUniqDigits }; diff --git a/src/utils/passRules.js b/src/utils/passRules.js new file mode 100644 index 00000000..50eab08c --- /dev/null +++ b/src/utils/passRules.js @@ -0,0 +1,22 @@ +'use strict'; + +const { hasUniqDigits } = require('./hasUniqDigits'); + +function passRules(num) { + const userInputStr = String(num); + + if ( + userInputStr.length !== 4 || + !hasUniqDigits(userInputStr) || + userInputStr[0] === '0' || + isNaN(userInputStr) + ) { + return false; + } + + return true; +} + +module.exports = { + passRules, +};