diff --git a/04week/mastermind.js b/04week/mastermind.js index 60e5cfa18..6d427b4a4 100644 --- a/04week/mastermind.js +++ b/04week/mastermind.js @@ -8,8 +8,9 @@ const rl = readline.createInterface({ }); let board = []; -let solution = ''; -let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; +let solution = 'abcd'; +let letters = ['a', 'b', 'c', 'd']; +let colors = require('colors'); function printBoard() { for (let i = 0; i < board.length; i++) { @@ -28,13 +29,41 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { - // your code here +function generateHint(solution, guess) { + + let solutionArray = solution.split(''); + + let guessArray = guess.split(''); + + let correctLetterLocations = 0; + let correctLetters = 0; + + for (let i = 0; i < solutionArray.length; i++) { + if (solutionArray[i] === guessArray[i]) { + correctLetterLocations = correctLetterLocations +1; + solutionArray[i] = null; + } + } + + for (let i = 0; i < solutionArray.length; i++) { + if (guessArray.indexOf([i]) === solutionArray[i]) { + let targetIndex = guessArray.indexOf([i]); + if (targetIndex > -1) { + correctLetters = correctLetters +1; + solutionArray[i] = null; + } + } + } + + return correctLetterLocations.red + correctLetters.white; + } + function mastermind(guess) { - solution = 'abcd'; // Comment this out to generate a random solution - // your code here + if (guess === solution) { + return 'you guessed it!'; + } } diff --git a/package-lock.json b/package-lock.json index bd07c0e56..71548816c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -553,9 +553,9 @@ } }, "colors": { - "version": "0.6.2", - "resolved": "https://registry.npmjs.org/colors/-/colors-0.6.2.tgz", - "integrity": "sha1-JCP+ZnisDF2uiFLl0OW+CMmXq8w=" + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.3.3.tgz", + "integrity": "sha512-mmGt/1pZqYRjMxB1axhTo16/snVZ5krrKkcmMeVKxzECMMXoCgnvTPp10QgHfcbQZw8Dq2jMNG6je4JlWU0gWg==" }, "colors-tmpl": { "version": "0.1.1", @@ -563,6 +563,13 @@ "integrity": "sha1-F+n5rYBceS/6OsagSctzJ9llt/4=", "requires": { "colors": "~0.6.2" + }, + "dependencies": { + "colors": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/colors/-/colors-0.6.2.tgz", + "integrity": "sha1-JCP+ZnisDF2uiFLl0OW+CMmXq8w=" + } } }, "combined-stream": { @@ -1728,15 +1735,14 @@ "version": "github:kevincolten/htmllint-cli#5901ac1f6cd612f484f40c7f4f7515f22a2ba52d", "from": "github:kevincolten/htmllint-cli", "requires": { - "bluebird": "^3.4.7", - "chalk": "^1.1.3", + "bluebird": "^3.5.1", + "chalk": "^2.3.0", "cjson": "^0.5.0", "glob": "^7.1.1", - "htmllint": "^0.6.0", - "liftoff": "^2.3.0", - "promise": "^7.1.1", - "semver": "^5.3.0", - "yargs": "^6.6.0" + "htmllint": "^0.7.0", + "liftoff": "^2.5.0", + "semver": "^5.4.1", + "yargs": "^10.0.3" }, "dependencies": { "ansi-styles": { diff --git a/package.json b/package.json index aff08712c..57080b13d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "homepage": "https://github.com/AustinCodingAcademy/javascript-workbook#readme", "dependencies": { + "colors": "^1.3.3", "eslint": "^3.19.0", "functional-javascript-workshop": "^1.0.6", "htmllint-cli": "github:kevincolten/htmllint-cli",