├── .gitignore ├── .nvmrc ├── LICENSE ├── README.md ├── package.json ├── src ├── assets │ ├── main.css │ └── main.js └── index.html └── test └── main.spec.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | 6 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Eric J Fisher 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Use JavaScript to Create a "Code Breaker" Game 2 | 3 | You'll build a Code Breaker game using JavaScript. Based off the board game Mastermind, the game will randomly generate a hidden code and the player gets 10 attempts to guess that code based on provided feedback. 4 | 5 |  6 | 7 | # Getting Started 8 | 9 | To get started with this project, head over to the [Link to Repository](https://www.codeschool.com/projects/CodeBreaker) project on Code School and begin! It'll walk you through all of the steps below. They're included here in the readme in case you work better locally or want to try working on this project offline. 10 | 11 | To get set up locally, run the following commands: 12 | 13 | ``` 14 | npm install 15 | npm start 16 | ``` 17 | 18 | # Live Demo 19 | 20 | [Check out this link] (https://codeschool-projects.github.io/CodeBreakerProject/) to see a working version of this project. Feel free to alter and expand on this project to make your own twist on the Code Breaker game once you've completed the steps. 21 | 22 | You'll build a Code Breaker game that you can play and show off to others as an example of your abilities in JavaScript. 23 | 24 | # Setup Instructions 25 | 26 | Once you have cloned the forked repository, go into the directory containing the project and look for the `/src` directory. This is the directory where you will be making changes when you start the following step-by-step instructions. You can simply open those files in any text editor to get started. 27 | 28 | In this project, all of your changes will happen in the `/src/assets/main.js` file. 29 | 30 | # Tasks 31 | 32 | Complete the following tasks to finish this project. 33 | 34 | ## Create `setHiddenFields` function 35 | 36 | Create a function named `setHiddenFields` that sets the `answer` variable equal to a randomly generated whole number between 0 and 9999. 37 | 38 | **Hint:** `Math.random()` can be used to randomly generate a number between 0 and 1 (up to 18 decimal points) and `Math.floor(input)` can be used to round down to the nearest whole number. 39 | 40 | ## Make sure the hidden input `answer`'s value is exactly 4 characters long 41 | 42 | In our `setHiddenFields` function we need to make sure the hidden input `answer` is exactly 4 characters long. (If our random number generates "42", we want to set the `value` of `answer` to "0042".) 43 | 44 | **Hint:** In order to add a zero to the front of an answer, it must be a string, not a number. You can convert numbers to strings with `.toString()`. We can create a `while` loop that runs while `answer.length` is less than 4 that puts a `0` before answer's current value. 45 | 46 | ## Set the hidden input `attempt`'s value to zero 47 | 48 | In our `setHiddenFields` function, we should also set the hidden input `attempt` to `0`. 49 | 50 | ## Only set the `answer` and `attempt` hidden inputs when they aren't already set 51 | 52 | Call the `setHiddenFields` function in the body of the `guess` function, but also write some logic so that it's only called when answer and attempt haven't already been set. 53 | 54 | **Hint:** we can use an `if` condition to only run our code when `answer` or `attempt` is empty (`''`). 55 | 56 | ## Create `setMessage` function 57 | 58 | Create a `setMessage` function with one parameter. This function should set the `message` label to whatever is provided to the parameter. 59 | 60 | **Hint:** With a label, you'll want to set its `.innerHTML`, not its `.value`. 61 | 62 | ## Create `validateInput` function 63 | 64 | Create a function `validateInput` with one parameter. If the parameter has a `length` of 4, return `true` — otherwise, use the `setMessage` function to set the `message` label to `"Guesses must be exactly 4 characters long."`, then return `false`. 65 | 66 | ## Call the `validateInput` function from the `guess` function 67 | 68 | Create an `if` condition block that uses `validateInput` with a parameter of `input.value` as the conditional. If `validateInput` returns `false`, then use `return false` to stop execution of the `guess` function, otherwise we should increment the `attempt` hidden input by 1. 69 | 70 | **Hint:** You can negate a value on the `if` statement by using the exclamation point, like this: `if(!someValue)`. 71 | 72 | ## Create `getResults` function 73 | 74 | Create a `getResults` function that has one parameter. In this function, we need to add the results of the user's guess to our `results` div's `innerHTML`. Each result should begin with `
17 | ???? 18 |
19 |Guess the randomly generated 4 digit code.
47 | 48 |