diff --git a/README.md b/README.md index e69d9d0..008da22 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ 1. Ensure you have installed all dependencies/packages: `npm i` 1. Look at the Unit Test, see what is being called, passed as input arguments, and what the expected result are. 1. Ensure you know how to run the unit test: - * `npm test main.js` + - `npm test main.js` 1. Use a whiteboard to work out a solution to building the Pig Latin program 1. Translate the broad ideas to psuedo code 1. Convert the psuedo code to real JavaScript Code @@ -31,7 +31,7 @@ 1. Look at your hints! 1. **Clone, setup, testing, and running instructions for all projects is below** -****** +--- ## Cloning Your Project @@ -39,15 +39,15 @@ 1. Copy HTTPS URL from your forked repository 1. In your terminal/gitBash/CommandPrompt navigate (using `cd`) into a directory where you want to start keeping your repositories. (`/jsDevFolder`) 1. Clone your new repository by typing `git clone ` (the HTTPS -URL you copied above) - ![Forking a repository](https://docs.google.com/drawings/d/1tYsLHaLo8JRdp0xC1EZrAo0o9Wvv4S5AD937cokVOBk/pub?w=960&h=720) + URL you copied above) + ![Forking a repository](https://docs.google.com/drawings/d/1tYsLHaLo8JRdp0xC1EZrAo0o9Wvv4S5AD937cokVOBk/pub?w=960&h=720) 1. Now go into the new directory by using `cd project-repo` 1. Add the base repository as an upstream - `git remote add upstream https://github.com/AustinCodingAcademy/.git` + `git remote add upstream https://github.com/AustinCodingAcademy/.git` 1. Check the configuration of your remotes with `git remote -v`, it should look -very similar to this (except it'll be YOUR username) + very similar to this (except it'll be YOUR username) ```bash $ git remote -v @@ -61,32 +61,32 @@ upstream git@github.com:AustinCodingAcademy/javascript-workbook.git (push) ### Setup 1. From your project directory, run `npm i` to tell NPM to install all the -node modules we use in this class (see `package.json`) + node modules we use in this class (see `package.json`) 1. Use your textEditor (VS Code) to change your files. 1. When you're finished `git status`, stage your file `git add .`, commit your changes `git commit -m "functions working"`, and push to -GitHub `git push` - ```bash - git status - git add . - git commit -m "Initial Commit" - git push origin gh-pages - ``` + GitHub `git push` + `bash + git status + git add . + git commit -m "Initial Commit" + git push origin gh-pages + ` 1. Now go to your forked repository on GitHub (at - https://github.com/your-username/javascript-workbook). A little yellow box - should have popped up asking you to make a Pull Request. Click to review. + https://github.com/your-username/javascript-workbook). A little yellow box + should have popped up asking you to make a Pull Request. Click to review. 1. Click "Create Pull Request" -1. Every time you make a change *and push to GitHub*, this PR will automatically -update. No need to do it more than once. +1. Every time you make a change _and push to GitHub_, this PR will automatically + update. No need to do it more than once. #### Get latest workbook updates 1. To get the latest code/homework/test updates, be sure to have a "clean -working directory" by committing or removing all of your changes. You check for -a "clean working environment" by running `git status` and making sure no files -show up. + working directory" by committing or removing all of your changes. You check for + a "clean working environment" by running `git status` and making sure no files + show up. 1. Run `git pull upstream gh-pages` @@ -115,3 +115,21 @@ Simply run `npm run lint` 1. Run `npm start` 1. To break out of the server, press `ctrl` + `c` + +Actual stuff for this assignment: + +Spec 0 - Define a test solution: Helpful suggestion: while developing you can set a default solution for you to test against. At the top of mastermind(), simply set const solution = 'abcd'; as a global variable. + +Spec 1 - Detect a correct solution: In mastermind(), if the guess you passed in equals the solution, return 'You guessed it!'; Spec 2 - Generate a hint: generateHint() should take one argument, guess. + +Spec 2.1 - Split up the solution and guess: In generateHint(), create variables solutionArray and guessArray that each split up passed in arguments, [.split](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/splitting on '' (empty string). + +Spec 2.2 - Determine correct "letter-locations": Create a variable correctLetterLocations and set it to 0. This variable will record how many correct "letter-locations" were guessed. For instance, a guess of aabc against a solution of deba would yield one correct "letter-location" (b). In a for loop, iterate over the solutionArray, comparing each index of solutionArray against the same index of guessArray. If the item matches, increment correctLetterLocations, and set that index in solutionArray to null. + +Spec 2.3 - Determine correct "letters": Now that we have nulled the already counted correctLetterLocations, we can see if the guessArray contains any correctLetters that were not in the correct location. Set a variable correctLetters equal to 0, and in a for loop, again iterate over the solutionArray. Using .indexOf, determine if the item at the current index in guessArray appears inside of solutionArray. Save that index in a variable called targetIndex. Now, if targetIndex is greater than -1(it exists in the array), increment correctLetters and set the item in solutionArray at that index equal to null. + +Spec 2.4 - return hint string: Optionally, you can use the colors package, return a string that prints out the hints you generated, with correctLetterLocations being red, correctLetters being white, and separated by a hyphen. > (NOTE: If you choose to use this color package, only console.log the result. If you return the result your program will fail the tests.) + +Spec 3 - Add guess and hint to the board: Define a variable called hint that collects the returned value of generateHint(guess). .push the guess and the hint (as a combined string) into the board. + +Spec 4 - End the game: After 10 incorrect guesses, if the board length equals 10, return 'You ran out of turns! The solution was' and the solution. Otherwise, return 'Guess again.'. diff --git a/main.js b/main.js index 64ca54b..2c3bffc 100644 --- a/main.js +++ b/main.js @@ -1,77 +1,111 @@ -'use strict'; +"use strict"; -const assert = require('assert'); -const readline = require('readline'); +const assert = require("assert"); +const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, - output: process.stdout + output: process.stdout, }); let board = []; -let solution = ''; -let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; +let solution = ""; +let letters = ["a", "b", "c", "d", "e", "f", "g", "h"]; -const printBoard = () => { +// let guess = 0; +// const hint = []; + +const printBoard = () => { for (let i = 0; i < board.length; i++) { console.log(board[i]); } -} +}; -const generateSolution = () => { +const generateSolution = () => { for (let i = 0; i < 4; i++) { const randomIndex = getRandomInt(0, letters.length); solution += letters[randomIndex]; } -} +}; const getRandomInt = (min, max) => { return Math.floor(Math.random() * (max - min)) + min; -} +}; -const generateHint = () => { +const generateHint = (guess) => { // your code here -} + // solution still exists in global scope + const guessArray = guess.split(""); + const solutionArray = solution.split(""); + let correctLetterLocations = 0; + for (let i = 0; i < solutionArray.length; i++) { + if (guessArray[i] === solutionArray[i]) { + correctLetterLocations++; + solutionArray[i] = null; + } + } + let correctLetters = 0; + for (let i = 0; i < solutionArray.length; i++) { + // if (guessArray.indexOf(solutionArray[i]) > -1) { + // correctLetters++; + // solutionArray[i] = null; + // } + + let targetIndex = solutionArray.indexOf(guessArray[i]); + if (targetIndex > -1) { + correctLetters++; + solutionArray[targetIndex] = null; + } + } + return `${correctLetterLocations}-${correctLetters}`; +}; const mastermind = (guess) => { - solution = 'abcd'; // Comment this out to generate a random solution + solution = "abcd"; // Comment this out to generate a random solution // your code here -} - + if (guess === solution) { + return "You guessed it!"; + } else { + let hint = generateHint(guess); + board.push(`${guess} ${hint}`); + if (board.length === 10) { + return `You ran out of turns! The solution was ${solution}`; + } else { + return `Guess again.`; + } + } +}; -const getPrompt = () => { - rl.question('guess: ', (guess) => { +const getPrompt = () => { + rl.question("guess: ", (guess) => { mastermind(guess); printBoard(); getPrompt(); }); -} +}; // Tests -if (typeof describe === 'function') { - solution = 'abcd'; - describe('#mastermind()', () => { - it('should register a guess and generate hints', () => { - mastermind('aabb'); +if (typeof describe === "function") { + solution = "abcd"; + describe("#mastermind()", () => { + it("should register a guess and generate hints", () => { + mastermind("aabb"); assert.equal(board.length, 1); }); - it('should be able to detect a win', () => { - assert.equal(mastermind(solution), 'You guessed it!'); + it("should be able to detect a win", () => { + assert.equal(mastermind(solution), "You guessed it!"); }); }); - describe('#generateHint()', () => { - it('should generate hints', () => { - assert.equal(generateHint('abdc'), '2-2'); + describe("#generateHint()", () => { + it("should generate hints", () => { + assert.equal(generateHint("abdc"), "2-2"); }); - it('should generate hints if solution has duplicates', () => { - assert.equal(generateHint('aabb'), '1-1'); + it("should generate hints if solution has duplicates", () => { + assert.equal(generateHint("aabb"), "1-1"); }); - }); - } else { - generateSolution(); getPrompt(); -} \ No newline at end of file +}