├── CONTRIBUTING.md ├── IssueTemplates.md ├── LICENSE ├── README.md ├── Week1 ├── README.md ├── homework │ ├── code │ │ └── solution │ │ │ ├── fizzbuzz.js │ │ │ └── twilioquest-code │ │ │ ├── divideByTwo.js │ │ │ ├── laserConfiguration.js │ │ │ ├── laserFunction.js │ │ │ ├── politeLasers.js │ │ │ └── sayPlease.js │ └── issues │ │ ├── fizzbuzz.md │ │ └── github.md └── livestream │ └── loom.md ├── Week2 ├── README.md ├── homework │ ├── code │ │ └── solution │ │ │ ├── 2.1-romancalc-phase1.js │ │ │ ├── 2.2-romancalc-phase2.js │ │ │ ├── 2.3-romancalc-phase3.js │ │ │ ├── 2.4-romancalc-phase4.js │ │ │ ├── 2.5-romancalc-phase5.js │ │ │ ├── sentenceconcat.js │ │ │ ├── sumarray.js │ │ │ └── twilioquest-code │ │ │ ├── addFirstToLast.js │ │ │ ├── enhancedLifeDetector.js │ │ │ ├── freighterInventory.js │ │ │ ├── getFirstAmountSorted.js │ │ │ ├── northBridgeControl.js │ │ │ ├── shouldWater.js │ │ │ ├── sortOrder.js │ │ │ └── treeLifeDetector.js │ └── issues │ │ ├── sentenceconcat.md │ │ └── sumarray.md └── livestream │ ├── .DS_Store │ └── loom.md ├── Week3 ├── README.md ├── homework │ ├── code │ │ └── solution │ │ │ └── romanint.js │ └── issues │ │ ├── finalproject.md │ │ └── romanint.md └── livestream │ └── loom.md └── week4 ├── README.md └── examples └── TicTacToe.js /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | ## Course, Repo Format 3 | Each course consists of two large components, a weekly live stream and weekly asynchronous homework assignments. 4 | - Livestream :lightning_cloud: 5 | These are projects that the course facilitators will develop for students to learn the core skills and use as reference in completing homework and building their projects. 6 | - Homework 7 | Homework is assigned in the form of issues in a Github Learning Lab. The config.yml file describes the progression of the course, as students open and close issues to progress along with the curriculum. Follow this video to see the structure of the issue files in the course: 8 | 9 | 10 | 11 | 12 | ## Issues 13 | 14 | Issues (complaints, improvements, or recommendations) are very valuable to this project. 15 | 16 | * Ideas are a valuable source of contributions others can make 17 | * Problems show where this project is lacking 18 | * With a question, you show where contributors can improve the user experience 19 | 20 | Thank you for creating them. 21 | 22 | (See `IssueTemplates.md` for more guidance here) 23 | 24 | 25 | ## Pull Requests 26 | 27 | Pull requests are, a great way to get your ideas into this repository. 28 | 29 | When deciding if I merge in a pull request I look at the following things: 30 | 31 | ### Does it state intent 32 | You should be clear which problem you're trying to solve with your contribution. 33 | 34 | For example: 35 | 36 | > Add a link to code of conduct in README.md 37 | 38 | Doesn't tell us anything about why you're doing that 39 | 40 | > Add a link to code of conduct in README.md because users don't always look in the CONTRIBUTING.md 41 | 42 | Tells me the problem that you have found, and the pull request shows me the action you have taken to solve it. 43 | 44 | ### Is it of good quality 45 | * There are no spelling mistakes 46 | * It reads well 47 | * For English language contributions: Has a good score on Grammarly or Hemingway App 48 | 49 | ### Does it move this repository closer to my vision for the repository 50 | The aim of this repository is: 51 | 52 | * To provide a README.md and assorted documents anyone can copy and paste, into their project 53 | * The content is usable by someone who hasn't written something like this before 54 | * Foster a culture of respect and gratitude in the open-source community. 55 | 56 | ### Does it follow the contributor covenant 57 | This repository has a code of conduct, This repository has a code of conduct, I will remove things that do not respect it. 58 | -------------------------------------------------------------------------------- /IssueTemplates.md: -------------------------------------------------------------------------------- 1 | # Issue Templates 2 | This doc can help provide some guidance for your issue, making it easier to share your thoughts with the developers and to make sure we can get your suggestions added. 3 | 4 | We would be remise if we did not acknowledge, that after filing an issue, you may be the best person to submit a fix for that issue. We thrive off of the energy and spirit of collaboration that the open source community offers, and would love to have you make a contribution to the content we share with our students. 5 | 6 | That said, here are some easy templates to file your issue quickly. These are very general, so please feel free to edit them as appropriate 7 | 8 | 9 | ## Bug report: 10 | 11 | ### Expected Behavior 12 | 13 | 14 | ### Actual Behavior, Code Location 15 | 16 | 17 | ### Steps to Reproduce the Problem 18 | 19 | 1. 20 | 1. 21 | 1. 22 | 23 | ### Specifications 24 | 25 | - Version: 26 | - Platform: 27 | - Subsystem: 28 | 29 | 30 | 31 | 32 | ## Inquiry, Correction: 33 | 34 | ### Confusing Section 35 | 36 | 37 | ### Resources, Another Explanation 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Bit Project 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 | ## **Bitcamp-javascript** 2 | 3 | Learn about the versatility and power of the Javascript programming language, with a developer advocate from Twilio! 4 | 5 | **Why Learn Javascript?** 6 | 7 | Javascript is one of the most consistently requested languages to learn from students and new engineers, and it's no surprise. Javascript is commonly used for both frontend and backend operations, with dozens of popular, powerful libraries at its disposal. 8 | 9 | Twilio specifically is one of the leading sources for everything communications on the web. From their site: 10 | 11 | - Connect with customers everywhere they want to interact with you—from text messages to emails, phone calls to video, intelligent chatbots and back—within a single powerful platform. 12 | 13 | Learning Javascript gives you the ability to interact with all sorts of online tools pre-built for you, just like Twilio! 14 | 15 | **What will the student work on?** 16 | 17 | Students will quest online together through Space! The [TwilioQuest](https://www.twilio.com/quest/learn/javascript) modules online are excellent resources that you can follow along with during our weekly live sessions and ask questions of our experienced staff. 18 | 19 | Between live streams, there will be original take-home assignments that enforce the learnings from the modules and help you venture into your own future journeys through space and the Javascript universe! 20 | 21 | 22 | ## Learning Objectives: 23 | 24 | ### **Week 1** 25 | 26 | System setup and starting your journey! 27 | 28 | **Objective: *Suit up and Set off to Space*** 29 | 30 | - Enviornment setup 31 | - Defining all the components and terms 32 | - TwilioQuest, Javascript, editor 33 | - Initial few quests (hello world equivilents) 34 | 35 | **Homework** 36 | 37 | - Set up your environment for non-TwilioQuest development 38 | - Complete FizzBuzz (or an equally difficult problem) 39 | - Goal: get some more 'standard' problems outside of the game environment 40 | 41 | 42 | ### **Week 2** 43 | 44 | Strings, Arrays, and Functions, oh my! 45 | 46 | **Objective: *Start repairing the station*** 47 | 48 | - Continue with the quest 49 | - Complete tasks, getting used to javascript syntax for: 50 | - Strings and string functions 51 | - Arrays and Lists 52 | - Functions, parameters, and returns 53 | 54 | **Homework** 55 | 56 | - Complete some practice array challenges from [HackerRank](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays) (or similar) 57 | - Work with arrays in more typical, interview-prep way 58 | - Goal: Javascript coverage and learning some of what most developers will know after a year or two of learning\ 59 | 60 | ### **Week 3** 61 | 62 | Programs! Building functions that behave appropriately for the prompts that are passed in. 63 | 64 | **Objective: *Complete Your Quest*** 65 | 66 | - Complete the Game! 67 | - Finish the remaining tasks for the space station 68 | - Take some time to step back and think about programming more generally 69 | - See how the game's format parallels to standalone programming 70 | 71 | **Homework** 72 | 73 | - Complete some practice string challenges from Leetcode 74 | - Some examples are available like [this](https://leetcode.com/problems/roman-to-integer/) and [this](https://leetcode.com/problems/valid-palindrome/) (or similar) 75 | - Work with strings in more typical, interview-prep way 76 | - Goal: Javascript coverage and learning some of what most developers will know after a year or two of learning 77 | -------------------------------------------------------------------------------- /Week1/README.md: -------------------------------------------------------------------------------- 1 | # Week 1 FizzBuzz in Space 🚀 2 | 3 | ### Learning Outcomes: 4 | 5 | - Environment setup 6 | - Defining all the components and terms to accomplish the tasks 7 | - TwilioQuest, Javascript, editor, command line 8 | - Initial few quests in TwilioQuest 9 | - Building first standalone program for homework 10 | 11 | ### List of Needed Technologies: 12 | 13 | - A command line interface (any should work) 14 | - A text editor (any should work) 15 | 16 | ### Links to relevant resources: 17 | 18 | - ["FizzBuzz In Too Much Detail"](https://www.tomdalling.com/blog/software-design/fizzbuzz-in-too-much-detail/#:~:text=FizzBuzz%20is%20a%20very%20simple,numbers%20from%201%20to%20100.) 19 | 20 | 21 | ### Livestream 22 | 23 | In the livestream, we're going to be going over how to set up your computer to write javascript and how to use GitHub (the basics). 24 | 25 | - For the full video, look in the [video folder](https://github.com/bitprj/Bitcamp-javascript/blob/main/Week1/livestream/loom.md). 26 | - For the full code, look in the [code folder](https://github.com/bitprj/Bitcamp-javascript/tree/main/week1/homework/code/solution). 27 | 28 | We'll be going over how to: 29 | 30 | 1. Installing TwilioQuest 31 | 2. Set up your system to write javascript code 32 | 3. Learning the Basics of GitHub 33 | 34 | 35 | ### Homework 36 | 37 | Your task is to use the knowledge from the first few challenges in TwilioQuest to program FizzBuzz in space! After completing your program, you will also be submitting it to your own Github repository. 38 | -------------------------------------------------------------------------------- /Week1/homework/code/solution/fizzbuzz.js: -------------------------------------------------------------------------------- 1 | // FizzBuzz In SPACE 2 | const argumentValue = process.argv[2]; 3 | const numericValue = Number(argumentValue); 4 | 5 | // For every number 1 ... numericValue: 6 | var i; 7 | for (i = 1; i <= numericValue; i ++) { 8 | // if number is divisible by 3 but not 5, print 👽 9 | if (i % 3 == 0 && i % 5 != 0) { 10 | console.log("👽 " + i); 11 | 12 | // if number is divisible by 5 but not 3, print 🚀 13 | } else if (i % 5 == 0 && i % 3 != 0) { 14 | console.log("🚀 " + i); 15 | 16 | // if number is divisible by both 3 and 5, print 🛰 17 | } else if (i % 3 == 0 && i % 5 == 0) { 18 | console.log("🛰 " + i); 19 | 20 | // if number is not divisible by either 3 or 5, print 💩 21 | } else { 22 | console.log("💩 " + i); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Week1/homework/code/solution/twilioquest-code/divideByTwo.js: -------------------------------------------------------------------------------- 1 | const argumentValue = process.argv[2]; 2 | 3 | const numberValue = Number(argumentValue); 4 | 5 | const result = numberValue / 2; 6 | 7 | console.log(result) -------------------------------------------------------------------------------- /Week1/homework/code/solution/twilioquest-code/laserConfiguration.js: -------------------------------------------------------------------------------- 1 | const laserStatus = "OFF"; 2 | 3 | console.log(laserStatus); -------------------------------------------------------------------------------- /Week1/homework/code/solution/twilioquest-code/laserFunction.js: -------------------------------------------------------------------------------- 1 | function getLaserSetting(){ 2 | const setting = "OFF"; 3 | return setting; 4 | } 5 | 6 | const currentSetting = getLaserSetting(); 7 | console.log("The current setting is " + currentSetting); -------------------------------------------------------------------------------- /Week1/homework/code/solution/twilioquest-code/politeLasers.js: -------------------------------------------------------------------------------- 1 | function getLaserSetting(magicword){ 2 | if(magicword == 'please'){ 3 | return 'OFF'; 4 | } 5 | else { 6 | return 'ON'; 7 | } 8 | } 9 | 10 | const currentSetting = getLaserSetting("please"); 11 | console.log(currentSetting); -------------------------------------------------------------------------------- /Week1/homework/code/solution/twilioquest-code/sayPlease.js: -------------------------------------------------------------------------------- 1 | console.log('Glen, will you please open the barrier?'); -------------------------------------------------------------------------------- /Week1/homework/issues/fizzbuzz.md: -------------------------------------------------------------------------------- 1 | ## FizzBuzz In SPACE 2 | 3 | *For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.* 4 | 5 | ### FizzBuzz 6 | 7 | FizzBuzz is a program used to demonstrate basic functionality of a language, often when switching between programing languages or as an introduction (that's us!) to first learning a language. 8 | 9 | The goals of the program is to go through the numbers from 1 to 100 and print "fizz" whenever a number is divisible by 3, print "buzz" when it's divisible by 5, print "fizzbuzz" when it's divisible by both 3 and 5, and print the number when it's not divisible by either. For our program, you will do something very similar: 10 | 11 | For the numbers from 1 to 100: 12 | * print 👽 and the number if it is divisible by 3 13 | * print 🚀 and the number if it is divisible by 5 14 | * print 🛰 and the number if it is divisible by both 3 and 5 15 | * print 💩 and the number if it is not divisible by either 3 or 5 16 | 17 | Tip: copy paste the emojis to get it into your code! 18 | 19 | 20 | **Task 1: Complete the FizzBuzz program as described above** 21 | 22 | 23 | 24 | Good job, you're fizzing and buzzing in space! 25 | -------------------------------------------------------------------------------- /Week1/homework/issues/github.md: -------------------------------------------------------------------------------- 1 | ## Learning GitHub 2 | 3 | *For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.* 4 | 5 | ### GitHub 6 | 7 | GitHub is a cloud-based repository hosting service that is widely used in the tech industry. It allows teams to use Git for version control, collaboration, and file management. If you don't know what Git is, it's a version-control system for tracking changes, managing state, and concurrently developing on the same files or directories. Git and Github's tools are specifically designed to make coordinating work easier, and they are one of the most pervasive shared tools among students and the industry. 8 | 9 | If you want to learn more about *what it is* and *how to use it*, try taking [this](https://lab.github.com/githubtraining/introduction-to-github#:~:text=Audience.%201%20Assign%20yourself.%20Assign%20the%20first%20issue,an%20issue.%20%204%20Create%20a%20branch.%20) GitHub Learning Lab Course. After finishing it, you will have a strong understanding of all the features GitHub has to offer. To make an account, go to: https://github.com/join to sign up. After making an account, you're all set to complete Task 3! 10 | 11 | 12 | 13 | **Task 2: create a repository and commit your homework javascript file to it (make sure you know key functions like commits, forking, pull request, branch, etc).** 14 | 15 | 16 | 17 | Good job, you're done with both parts! 18 | -------------------------------------------------------------------------------- /Week1/livestream/loom.md: -------------------------------------------------------------------------------- 1 | [Intro to JavaScript Week 1](https://youtu.be/w_kB43gwov8) -------------------------------------------------------------------------------- /Week2/README.md: -------------------------------------------------------------------------------- 1 | # Week 2: Array Fun 2 | 3 | ### Learning Outcomes 4 | - Get comfortable with JavaScript syntax for: 5 | - Strings and string Functions 6 | - Arrays 7 | - Lists 8 | - Functions, parameters, and returns 9 | 10 | ### List of needed technologies: 11 | - A command line interface 12 | - A text editor 13 | 14 | ### Link to relevant resources: 15 | - [JavaScript Functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions) 16 | 17 | 18 | ### Livestream 19 | 20 | We'll be going over how to: 21 | 22 | 1. Write loops in javascript 23 | 2. Write functions in javascript 24 | 3. Work with strings in javaScript 25 | 26 | - For the full video, look in the [video folder](https://github.com/bitprj/Bitcamp-javascript/blob/main/Week2/livestream/loom.md). 27 | - For the full code, look in the [code folder](https://github.com/bitprj/Bitcamp-javascript/tree/main/Week2/homework/code/solution). 28 | 29 | 30 | ### Homework 31 | 32 | Your task is to use the knowledge from the challenges in TwilioQuest to program two small functions using loops and strings! 33 | -------------------------------------------------------------------------------- /Week2/homework/code/solution/2.1-romancalc-phase1.js: -------------------------------------------------------------------------------- 1 | // Initialize an array that will hold the values to be calculated (from the command line) 2 | function get_args() { 3 | const operand1 = process.argv[2]; 4 | const operator = process.argv[3]; 5 | const operand2 = process.argv[4]; 6 | let op = [operand1, operator, operand2]; 7 | return op; 8 | } 9 | 10 | // Test cases: 11 | /* run romancalc.js a b c*/ 12 | let op = get_args(); 13 | console.log("The first operand is: ", op[0]); 14 | console.log("The second operand is: ", op[2]); 15 | console.log("The operator is: ", op[1]); -------------------------------------------------------------------------------- /Week2/homework/code/solution/2.2-romancalc-phase2.js: -------------------------------------------------------------------------------- 1 | //PHASE 1: 2 | 3 | // Initialize an array that will hold the values to be calculated (from the command line) 4 | function get_args() { 5 | var operand1 = process.argv[2]; 6 | var operator = process.argv[3]; 7 | var operand2 = process.argv[4]; 8 | let op = [operand1, operator, operand2]; 9 | return op; 10 | } 11 | 12 | // PHASE 2: 13 | 14 | function calculate() { 15 | // Use previously built get_args function to get operators and operand 16 | let op = get_args(); 17 | var a = Number(op[0]); 18 | var operator = op[1]; 19 | var b = Number(op[2]); 20 | 21 | // Initialize variable to hold result of operations 22 | var result = 0; 23 | 24 | // Use conditions to determine what the operation is from the user input and calculate the result 25 | if(operator == '+') { 26 | result = a + b; 27 | } else if(operator == '-') { 28 | result = a - b; 29 | } else if(operator == 'x') { 30 | result = a * b; 31 | } else if(operator == '/') { 32 | result = a / b; 33 | } else if(operator == '^') { 34 | result = a ** b; 35 | } else if(operator == '%') { 36 | result = a % b; 37 | } 38 | 39 | return result; 40 | } 41 | 42 | // Testing: 43 | /* 44 | Run romancalc.js a b c 45 | where a and c are numbers 46 | and b is the operator. 47 | This code is sensitive to the spacing in the user input (command line). 48 | */ 49 | console.log(calculate()); -------------------------------------------------------------------------------- /Week2/homework/code/solution/2.3-romancalc-phase3.js: -------------------------------------------------------------------------------- 1 | //PHASE 1: 2 | 3 | // Initialize an array that will hold the values to be calculated (from the command line) 4 | function get_args() { 5 | const operand1 = process.argv[2]; 6 | const operator = process.argv[3]; 7 | const operand2 = process.argv[4]; 8 | let op = [operand1, operator, operand2]; 9 | return op; 10 | } 11 | 12 | // PHASE 2: 13 | 14 | function calculate() { 15 | // Use previously built get_args function to get operators and operand 16 | let op = get_args(); 17 | var a = Number(op[0]); 18 | var operator = op[1]; 19 | var b = Number(op[2]); 20 | 21 | // Initialize variable to hold result of operations 22 | var result = 0; 23 | 24 | // Use conditions to determine what the operation is from the user input and calculate the result 25 | if(operator == '+') { 26 | result = a + b; 27 | } else if(operator == '-') { 28 | result = a - b; 29 | } else if(operator == 'x') { 30 | result = a * b; 31 | } else if(operator == '/') { 32 | result = a / b; 33 | } else if(operator == '^') { 34 | result = a ^ b; 35 | } else if(operator == '%') { 36 | result = a % b; 37 | } 38 | 39 | return result; 40 | } 41 | 42 | // PHASE 3: 43 | 44 | // Learn more about associative arrays in JS: https://www.xul.fr/javascript/associative.php 45 | const letterVals = { 46 | "I": 1, 47 | "V": 5, 48 | "X": 10, 49 | "L": 50, 50 | "C": 100, 51 | "M": 1000 52 | }; 53 | 54 | // Function (romanToInt) that takes input as a parameter and returns int from roman value, ex. XXVII: 27, XIV: 14 55 | function romanToInt(input) { 56 | if (input === "") { 57 | return 0; 58 | } 59 | romanArr = input.split(""); 60 | 61 | len = romanArr.length 62 | value = 0; 63 | romanArr.forEach((item, index) => { 64 | if (item === "I" && index + 1 < len && 65 | (romanArr[index + 1] === "V" || romanArr[index + 1] === "X")) { 66 | // I can be placed before V (5) and X (10) to make 4 and 9 67 | value -= 2 * letterVals[item]; 68 | } else if (item === "X" && index + 1 < len && 69 | (romanArr[index + 1] === "L" || romanArr[index + 1] === "C")) { 70 | // X can be placed before L (50) and C (100) to make 40 and 90. 71 | value -= 2 * letterVals[item]; 72 | } else if (item === "C" && index + 1 < len && 73 | (romanArr[index + 1] === "D" || romanArr[index + 1] === "M")) { 74 | // C can be placed before D (500) and M (1000) to make 400 and 900 75 | value -= 2 * letterVals[item]; 76 | } 77 | 78 | value += letterVals[item]; 79 | }); 80 | 81 | return value; 82 | } 83 | 84 | // Testing 85 | console.log(romanToInt(process.argv[2])); 86 | //console.log(romanToInt("4")); 87 | //console.log(romanToInt("IV")); 88 | //console.log(romanToInt("XIV")); 89 | //console.log(romanToInt("XXVII")); -------------------------------------------------------------------------------- /Week2/homework/code/solution/2.4-romancalc-phase4.js: -------------------------------------------------------------------------------- 1 | //PHASE 1: 2 | 3 | // Initialize an array that will hold the values to be calculated (from the command line) 4 | function get_args() { 5 | const operand1 = process.argv[2]; 6 | const operator = process.argv[3]; 7 | const operand2 = process.argv[4]; 8 | let op = [operand1, operator, operand2]; 9 | return op; 10 | } 11 | 12 | // PHASE 2: 13 | 14 | function calculate() { 15 | // Use previously built get_args function to get operators and operand 16 | let op = get_args(); 17 | var a = Number(op[0]); 18 | var operator = op[1]; 19 | var b = Number(op[2]); 20 | 21 | // Initialize variable to hold result of operations 22 | var result = 0; 23 | 24 | // Use conditions to determine what the operation is from the user input and calculate the result 25 | if(operator == '+') { 26 | result = a + b; 27 | } else if(operator == '-') { 28 | result = a - b; 29 | } else if(operator == 'x') { 30 | result = a * b; 31 | } else if(operator == '/') { 32 | result = a / b; 33 | } else if(operator == '^') { 34 | result = a ^ b; 35 | } else if(operator == '%') { 36 | result = a % b; 37 | } 38 | 39 | return result; 40 | } 41 | 42 | // PHASE 3: 43 | 44 | // Learn more about associative arrays in JS: https://www.xul.fr/javascript/associative.php 45 | const letterVals = { 46 | "I": 1, 47 | "V": 5, 48 | "X": 10, 49 | "L": 50, 50 | "C": 100, 51 | "M": 1000 52 | }; 53 | 54 | // Function (romanToInt) that takes input as a parameter and returns int from roman value, ex. XXVII: 27, XIV: 14 55 | function romanToInt(input) { 56 | if (input === "") { 57 | return 0; 58 | } 59 | romanArr = input.split(""); 60 | 61 | len = romanArr.length 62 | value = 0; 63 | romanArr.forEach((item, index) => { 64 | if (item === "I" && index + 1 < len && 65 | (romanArr[index + 1] === "V" || romanArr[index + 1] === "X")) { 66 | // I can be placed before V (5) and X (10) to make 4 and 9 67 | value -= 2 * letterVals[item]; 68 | } else if (item === "X" && index + 1 < len && 69 | (romanArr[index + 1] === "L" || romanArr[index + 1] === "C")) { 70 | // X can be placed before L (50) and C (100) to make 40 and 90. 71 | value -= 2 * letterVals[item]; 72 | } else if (item === "C" && index + 1 < len && 73 | (romanArr[index + 1] === "D" || romanArr[index + 1] === "M")) { 74 | // C can be placed before D (500) and M (1000) to make 400 and 900 75 | value -= 2 * letterVals[item]; 76 | } 77 | 78 | value += letterVals[item]; 79 | }); 80 | 81 | return value; 82 | } 83 | 84 | // https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript 85 | function intToRoman(input) { 86 | if (isNaN(input)) 87 | return NaN; 88 | var digits = String(+input).split(""), 89 | key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM", 90 | "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC", 91 | "","I","II","III","IV","V","VI","VII","VIII","IX"], 92 | roman = "", 93 | i = 3; 94 | while (i--) 95 | roman = (key[+digits.pop() + (i * 10)] || "") + roman; 96 | return Array(+digits.join("") + 1).join("M") + roman; 97 | } 98 | 99 | // Testing 100 | console.log(intToRoman(process.argv[2])); -------------------------------------------------------------------------------- /Week2/homework/code/solution/2.5-romancalc-phase5.js: -------------------------------------------------------------------------------- 1 | function isNumber(n) { return /^-?[\d.]+(?:e-?\d+)?$/.test(n); } 2 | 3 | // PHASES 3 & 4 shifted to top: 4 | 5 | // Learn more about associative arrays in JS: https://www.xul.fr/javascript/associative.php 6 | const letterVals = { 7 | "I": 1, 8 | "V": 5, 9 | "X": 10, 10 | "L": 50, 11 | "C": 100, 12 | "M": 1000 13 | }; 14 | 15 | // Function (romanToInt) that takes romanString as a parameter and returns int from roman value, ex. XXVII: 27, XIV: 14 16 | function romanToInt(romanString) { 17 | if (romanString === "") { 18 | return 0; 19 | } 20 | romanArr = romanString.split(""); 21 | 22 | len = romanArr.length 23 | value = 0; 24 | romanArr.forEach((item, index) => { 25 | if (item === "I" && index + 1 < len && 26 | (romanArr[index + 1] === "V" || romanArr[index + 1] === "X")) { 27 | // I can be placed before V (5) and X (10) to make 4 and 9 28 | value -= 2 * letterVals[item]; 29 | } else if (item === "X" && index + 1 < len && 30 | (romanArr[index + 1] === "L" || romanArr[index + 1] === "C")) { 31 | // X can be placed before L (50) and C (100) to make 40 and 90. 32 | value -= 2 * letterVals[item]; 33 | } else if (item === "C" && index + 1 < len && 34 | (romanArr[index + 1] === "D" || romanArr[index + 1] === "M")) { 35 | // C can be placed before D (500) and M (1000) to make 400 and 900 36 | value -= 2 * letterVals[item]; 37 | } 38 | 39 | value += letterVals[item]; 40 | }); 41 | 42 | return value; 43 | } 44 | 45 | // https://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript 46 | function intToRoman(input) { 47 | if (isNaN(input)) 48 | return NaN; 49 | var digits = String(+input).split(""), 50 | key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM", 51 | "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC", 52 | "","I","II","III","IV","V","VI","VII","VIII","IX"], 53 | roman = "", 54 | i = 3; 55 | while (i--) 56 | roman = (key[+digits.pop() + (i * 10)] || "") + roman; 57 | return Array(+digits.join("") + 1).join("M") + roman; 58 | } 59 | 60 | //PHASE 1 & PHASE 5: 61 | 62 | // Initialize an array that will hold the values to be calculated (from the command line) 63 | function get_args() { 64 | if (!isNumber(process.argv[2])) { 65 | var operand1 = romanToInt(process.argv[2]); 66 | } else { 67 | var operand1 = process.argv[2]; 68 | } 69 | 70 | if(!isNumber(process.argv[4])) { 71 | var operand2 = romanToInt(process.argv[4]); 72 | } else { 73 | var operand2 = process.argv[4]; 74 | } 75 | 76 | var operator = process.argv[3]; 77 | let op = [operand1, operator, operand2]; 78 | return op; 79 | } 80 | 81 | // PHASE 2: 82 | 83 | function calculate() { 84 | // Use previously built get_args function to get operators and operand 85 | let op = get_args(); 86 | var a = Number(op[0]); 87 | var operator = op[1]; 88 | var b = Number(op[2]); 89 | 90 | // Initialize variable to hold result of operations 91 | var result = 0; 92 | 93 | // Use conditions to determine what the operation is from the user input and calculate the result 94 | if(operator == '+') { 95 | result = a + b; 96 | } else if(operator == '-') { 97 | result = a - b; 98 | } else if(operator == 'x') { 99 | result = a * b; 100 | } else if(operator == '/') { 101 | result = a / b; 102 | } else if(operator == '^') { 103 | result = a ** b; 104 | } else if(operator == '%') { 105 | result = a % b; 106 | } 107 | 108 | return result; 109 | } 110 | 111 | var result = calculate(); 112 | op = get_args(); 113 | 114 | console.log("Results:"); 115 | console.log(result); 116 | console.log(intToRoman(result)); -------------------------------------------------------------------------------- /Week2/homework/code/solution/sentenceconcat.js: -------------------------------------------------------------------------------- 1 | //write a function that creates a sentence from an array of strings 2 | 3 | function sentence(strArray) { 4 | var sentence = ""; 5 | strArray.forEach(function(item, index, array) { 6 | sentence = sentence.concat(item, " "); 7 | }) 8 | sentence = sentence.concat("."); 9 | return sentence; 10 | } 11 | 12 | //call your function using "words" as parameter 13 | let words = ["JavaScript", "is", "so", "awesome" , "👾"]; 14 | console.log(sentence(words)); 15 | -------------------------------------------------------------------------------- /Week2/homework/code/solution/sumarray.js: -------------------------------------------------------------------------------- 1 | //write a function that gets the sum of integers in an array 2 | 3 | function sum(numArray) { 4 | //loop through the array 5 | var sum = 0; 6 | numArray.forEach(function(item, index, array){ 7 | sum += item; 8 | }) 9 | return sum; 10 | } 11 | 12 | //call your function using 'numbers' array as parameter 13 | let numbers = [3,5,1,9,12]; 14 | console.log("The sum is", sum(numbers)); 15 | -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/addFirstToLast.js: -------------------------------------------------------------------------------- 1 | function addFirstToLast(stringArray) { 2 | if (stringArray.length === 0) { 3 | return ""; 4 | } else { 5 | return stringArray[0] + stringArray[stringArray.length-1]; 6 | } 7 | } -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/enhancedLifeDetector.js: -------------------------------------------------------------------------------- 1 | const treeStatus = process.argv[2]; 2 | 3 | if (treeStatus === '0') { 4 | console.log("alive"); 5 | } else if (treeStatus === "1") { 6 | console.log("flowering"); 7 | } else if (treeStatus === "2") { 8 | console.log("shedding"); 9 | } else { 10 | console.log("other"); 11 | } -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/freighterInventory.js: -------------------------------------------------------------------------------- 1 | const inventory = ["Ducktypium Ore", "Uranium Rod", "Ruthenium Isotopes", "Concave Lens", "Refraction Panel"] -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/getFirstAmountSorted.js: -------------------------------------------------------------------------------- 1 | function getFirstAmountSorted(stringArray, number){ 2 | stringArray.sort(); 3 | //console.log(stringArray); 4 | const slicedArray = stringArray.slice(0, number); 5 | return slicedArray; 6 | } 7 | 8 | const newArray = getFirstAmountSorted(['cat', 'apple', 'bat'], 2); 9 | console.log(newArray); -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/northBridgeControl.js: -------------------------------------------------------------------------------- 1 | const command = process.argv[2]; 2 | 3 | if (command === "EXTEND") { 4 | console.log("Extending bridge!"); 5 | } -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/shouldWater.js: -------------------------------------------------------------------------------- 1 | const lifeStatus = process.argv[2]; 2 | const dryLevel = process.argv[3]; 3 | const dryNumber = Number(dryLevel); 4 | 5 | if (lifeStatus === '0' && dryNumber > 10) { 6 | console.log("WATER"); 7 | } -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/sortOrder.js: -------------------------------------------------------------------------------- 1 | const firstWord = process.argv[2].toLowerCase(); 2 | const secondWord = process.argv[3].toLowerCase(); 3 | 4 | if (firstWord < secondWord) { 5 | console.log(-1); 6 | } else if (firstWord > secondWord) { 7 | console.log(1); 8 | } else { 9 | console.log(0); 10 | } -------------------------------------------------------------------------------- /Week2/homework/code/solution/twilioquest-code/treeLifeDetector.js: -------------------------------------------------------------------------------- 1 | const treeStatus = process.argv[2]; 2 | 3 | if (treeStatus === '0') { 4 | console.log("alive"); 5 | } else { 6 | console.log("other"); 7 | } -------------------------------------------------------------------------------- /Week2/homework/issues/sentenceconcat.md: -------------------------------------------------------------------------------- 1 | ## Sentence Concat 2 | 3 | *For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.* 4 | 5 | ### Concatenating words in a sentence 6 | 7 | Create a function that, given an array of strings, returns a sentence of all the words in that order. That is, concatenate all of the provided strings with spaces in between and a period at the end. 8 | 9 | Use what you learned from the TwilioQuest streams so far to write a function, write a for loop over the input strings, and return the final sentence! 10 | 11 | **Task 2: Complete a function that creates a sentence as described above!** 12 | 13 | 14 | Great job, the robots can speak now, and they keep asking for more batteries! 15 | -------------------------------------------------------------------------------- /Week2/homework/issues/sumarray.md: -------------------------------------------------------------------------------- 1 | ## Sum array 2 | 3 | *For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.* 4 | 5 | ### Summing the numbers in an array 6 | 7 | Create a function that, given an array of integers, returns the sum of those numbers added together. 8 | 9 | Use what you learned from the TwilioQuest streams so far to write a function, write a for loop over the numbers, and return the values! 10 | 11 | **Task 1: Complete a function that sums up the integers in a given array!** 12 | 13 | 14 | Great job, step one: adding machines, step two: profit! 15 | -------------------------------------------------------------------------------- /Week2/livestream/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bitprj/Bitcamp-JavaScript/65c8fe94533de3b2d37365a8b59a023e540322dc/Week2/livestream/.DS_Store -------------------------------------------------------------------------------- /Week2/livestream/loom.md: -------------------------------------------------------------------------------- 1 | [Intro to JavaScript Week 2](https://www.youtube.com/watch?v=VHiFSuhhgC0&ab_channel=BitProject) -------------------------------------------------------------------------------- /Week3/README.md: -------------------------------------------------------------------------------- 1 | # Week 3: Space-ware Engineering 2 | 3 | ### Learning Outcomes 4 | - Get comfortable with JavaScript syntax for: 5 | - Full programs 6 | - Strings and string Functions, Arrays, and Lists 7 | - Functions, parameters, and returns 8 | - Prepare for your final project next week 9 | 10 | ### List of needed technologies: 11 | - A command line interface 12 | - A text editor 13 | 14 | ### Link to relevant resources: 15 | - [Everything there is to know about javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) 16 | 17 | 18 | ### Livestream 19 | 20 | We'll be going over how to write full programs in javascript, bringing together everything you've learned so far playing TwilioQuest. It is okay if you don't finish the game itself, but if you're stuck on the homework or coming up with ideas for your final project, that would be a good place to start. 21 | 22 | - For the full video, look in the [video folder](https://github.com/bitprj/Bitcamp-javascript/blob/main/Week3/livestream/loom.md). 23 | - For the full code, look in the [code folder](https://github.com/bitprj/Bitcamp-javascript/tree/main/Week3/homework/code/solution). 24 | 25 | 26 | ### Homework 27 | 28 | Your task is to complete one larger program (Roman to Int) and complete as much of TwilioQuest as you can through for the week. We will be exclusively working on the final project next week, so you will also have a brainstorming session assigned with your mentor this week. Please come prepared to that session with ideas for what you want to make in your final week! 29 | -------------------------------------------------------------------------------- /Week3/homework/code/solution/romanint.js: -------------------------------------------------------------------------------- 1 | // Roman to integer, (maybe) integer to string, re: https://leetcode.com/problems/roman-to-integer/ 2 | 3 | const letterVals = { 4 | "I": 1, 5 | "V": 5, 6 | "X": 10, 7 | "L": 50, 8 | "C": 100, 9 | "D": 500, 10 | "M": 1000 11 | }; 12 | 13 | // returns int from roman value, ex. XXVII: 27, XIV: 14 14 | function romanToInt(romanString) { 15 | if (romanString === "") { 16 | return 0; 17 | } 18 | romanArr = romanString.split(""); 19 | 20 | len = romanArr.length 21 | value = 0; 22 | romanArr.forEach((item, index) => { 23 | if (item === "I" && index + 1 < len && 24 | (romanArr[index + 1] === "V" || romanArr[index + 1] === "X")) { 25 | // I can be placed before V (5) and X (10) to make 4 and 9 26 | value -= 2 * letterVals[item]; 27 | } else if (item === "X" && index + 1 < len && 28 | (romanArr[index + 1] === "L" || romanArr[index + 1] === "C")) { 29 | // X can be placed before L (50) and C (100) to make 40 and 90. 30 | value -= 2 * letterVals[item]; 31 | } else if (item === "C" && index + 1 < len && 32 | (romanArr[index + 1] === "D" || romanArr[index + 1] === "M")) { 33 | // C can be placed before D (500) and M (1000) to make 400 and 900 34 | value -= 2 * letterVals[item]; 35 | } 36 | 37 | value += letterVals[item]; 38 | 39 | }); 40 | 41 | return value; 42 | 43 | } 44 | 45 | // Test cases: 46 | console.log(romanToInt("")); 47 | console.log(romanToInt("I")); 48 | console.log(romanToInt("IV")); 49 | console.log(romanToInt("XIV")); 50 | console.log(romanToInt("XXVII")); 51 | 52 | 53 | // returns words from int value, ex. 17: seventeen, 9: nine 54 | function intToString(num) { 55 | return; 56 | } 57 | -------------------------------------------------------------------------------- /Week3/homework/issues/finalproject.md: -------------------------------------------------------------------------------- 1 | ## Final Project 2 | 3 | *For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.* 4 | 5 | ### Brainstorming ideas for your final project 6 | 7 | We will be exclusively working on the final project next week, so you will also have a brainstorming session assigned with your mentor this week. Please come prepared to that session with ideas for what you want to make in your final week! 8 | 9 | Your job is to come prepared and take notes during that meeting, so we can discuss project ideas and go through an example project for the livestream in week 4. 10 | 11 | **Task 2: Get ready for your final project next week and learn from your mentor!** 12 | 13 | 14 | Great job, you're all set to keep questing after returning from the space shuttle. 15 | -------------------------------------------------------------------------------- /Week3/homework/issues/romanint.md: -------------------------------------------------------------------------------- 1 | ## Roman Numeral to Integer 2 | 3 | *For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.* 4 | 5 | ### Roman Numerals (problem description from LeetCode) 6 | 7 | Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 8 | 9 | Symbol Value 10 | I 1 11 | V 5 12 | X 10 13 | L 50 14 | C 100 15 | D 500 16 | M 1000 17 | 18 | For example, 2 is written as `II` in Roman numeral, just two one's added together. 12 is written as `XII`, which is simply `X + II`. The number 27 is written as `XXVII`, which is `XX + V + II`. 19 | 20 | Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used: 21 | 22 | `I` can be placed before `V` (5) and `X` (10) to make 4 and 9. 23 | `X` can be placed before `L` (50) and `C` (100) to make 40 and 90. 24 | `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900. 25 | Given an integer, convert it to a roman numeral. 26 | 27 | 28 | Example 1: 29 | 30 | Input: num = 3 31 | Output: `III` 32 | Example 2: 33 | 34 | Input: num = 4 35 | Output: `IV` 36 | Example 3: 37 | 38 | Input: num = 9 39 | Output: `IX` 40 | Example 4: 41 | 42 | Input: num = 58 43 | Output: `LVIII` 44 | Explanation: L = 50, V = 5, III = 3. 45 | Example 5: 46 | 47 | Input: num = 1994 48 | Output: `MCMXCIV` 49 | Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 50 | 51 | 52 | **Task 1: Complete the Roman to Int program as described above** 53 | 54 | 55 | 56 | Great job, you're teaching javascript to the Romans! 57 | -------------------------------------------------------------------------------- /Week3/livestream/loom.md: -------------------------------------------------------------------------------- 1 | Videos will be uploaded here after being streamed 2 | -------------------------------------------------------------------------------- /week4/README.md: -------------------------------------------------------------------------------- 1 | # Week 4: Final Projects 2 | 3 | ### Learning Outcomes 4 | - Complete a capstone-style project with the help of your mentor 5 | - See how projects in industry and in upperclassmen courses look and are built 6 | 7 | ### List of needed technologies: 8 | - A command line interface 9 | - A text editor 10 | - A strong control of using Google Search 11 | 12 | ### Link to relevant resources: 13 | - [Everything there is to know about javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) 14 | 15 | 16 | ### Livestream 17 | 18 | We'll be going over an example project and discussing as a class what project ideas everybody has. You can use this time as a second brainstorming session or as an opportunity to test your ideas with your classmates. 19 | 20 | - For the full video, look in the [video folder](https://github.com/bitprj/Bitcamp-javascript/blob/main/Week3/livestream/loom.md). 21 | - For the full code, look in the [code folder](https://github.com/bitprj/Bitcamp-javascript/tree/main/Week3/homework/code/solution). 22 | 23 | 24 | ### Homework 25 | 26 | Build that project! We will be showing off your projects at the end of the week and sharing them to our blog. Take as much time as you'd like to build your project, and make good use of your software engineering mentor! 27 | -------------------------------------------------------------------------------- /week4/examples/TicTacToe.js: -------------------------------------------------------------------------------- 1 | // Cite: tic tac toe from github: https://gist.github.com/dstreet/fdcfdf214cb66d7a1637 2 | 3 | 4 | var readline = require('readline'); 5 | 6 | var TicTacToe = function(readline) { 7 | 8 | /* 9 | * Private API 10 | * ------------------------------------------------------------------------ 11 | */ 12 | 13 | var _rl = null 14 | , _boardSize = 3 15 | , _boardData = {} 16 | , _playerMarks = ['x', 'o'] 17 | , _players = []; 18 | 19 | _init(readline); 20 | 21 | 22 | /** 23 | * Initializes the readline interface 24 | * @private 25 | * @param {Object} Readline module 26 | */ 27 | function _init(readline) { 28 | _rl = readline.createInterface({ 29 | input: process.stdin, 30 | output: process.stdout 31 | }); 32 | } 33 | 34 | /** 35 | * Builds the board data object with a 3x3 array and an array of 36 | * empty cells 37 | * @private 38 | */ 39 | function _buildBoard() { 40 | var data = { 41 | board: [], 42 | empty: [] 43 | }; 44 | 45 | for (var i = 0; i < _boardSize; i += 1) { 46 | var row = []; 47 | 48 | for (var k = 0; k < _boardSize; k += 1) { 49 | row.push(null); 50 | data.empty.push(i + ' ' + k); 51 | } 52 | 53 | data.board.push(row); 54 | } 55 | 56 | _boardData = data; 57 | } 58 | 59 | /** 60 | * Sets the players array with the types supplied, and markers (x and o) 61 | * chosen at random 62 | * @private 63 | */ 64 | function _setupPlayers(playerTypes) { 65 | var initialIndex = 0; 66 | 67 | if (playerTypes.length == 2) { 68 | initialIndex = Math.round(Math.random()); 69 | 70 | _players = [ 71 | { 72 | marker: _playerMarks[initialIndex], 73 | type: playerTypes[0] 74 | }, 75 | { 76 | marker: _playerMarks[(!initialIndex | 0)], 77 | type: playerTypes[1] 78 | } 79 | ] 80 | } 81 | } 82 | 83 | /** 84 | * Rotates the players. Switches which player is currently active 85 | * @private 86 | */ 87 | function _rotatePlayers() { 88 | _players.push( _players.shift() ); 89 | } 90 | 91 | /** 92 | * Delegate getting the player's position determined by their type 93 | * @private 94 | */ 95 | function _getPlayerMove() { 96 | var pos = []; 97 | 98 | if (_players[0].type == 'ai') { 99 | pos = _aiMove(_addPlayerToBoard); 100 | } else { 101 | pos = _getInput(_addPlayerToBoard); 102 | } 103 | } 104 | 105 | /** 106 | * Get the AI's position by choosing a random empty cell 107 | * @private 108 | */ 109 | function _aiMove(callback) { 110 | var index = -1 111 | , pos = []; 112 | 113 | // Choose a random index from the available positions 114 | index = Math.floor(Math.random() * _boardData.empty.length - 0); 115 | pos = _posFromString(_boardData.empty[index]); 116 | 117 | setTimeout(function() { 118 | callback(pos); 119 | }, 1000); 120 | } 121 | 122 | /** 123 | * Get the human's position from the user input 124 | * @private 125 | */ 126 | function _getInput(callback) { 127 | _rl.question('Player ' + _players[0].marker + '\'s move (enter row column): ', function(input) { 128 | 129 | // Ensure input is an available cell 130 | if (_boardData.empty.indexOf(input) != -1) { 131 | callback(_posFromString(input)); 132 | } else { 133 | _getInput(callback); 134 | } 135 | }); 136 | } 137 | 138 | /** 139 | * Parse a space separate string of '[row] [column]' as a board position 140 | * @private 141 | */ 142 | function _posFromString(str) { 143 | var pos = str.split(' '); 144 | 145 | pos.forEach(function(el, i, arr) { 146 | arr[i] = parseInt(el); 147 | }); 148 | 149 | return pos; 150 | } 151 | 152 | /** 153 | * Add the active player to the board, print the board, and check 154 | * for a winner. If no winner is found, start again. 155 | * @private 156 | * @param {Array} The player's position 157 | */ 158 | function _addPlayerToBoard(pos) { 159 | _addToBoard(_players[0], pos); 160 | 161 | console.log('\u001b[2J\u001b[0;0H'); 162 | _printBoard(); 163 | 164 | if (_boardData.empty.length == 0) { 165 | console.log('Draw!'); 166 | _rl.close(); 167 | return; 168 | } 169 | 170 | if (_checkBoard(_players[0])) { 171 | _rl.close(); 172 | return; 173 | } else { 174 | _rotatePlayers(); 175 | _getPlayerMove(_players[0]); 176 | } 177 | } 178 | 179 | /** 180 | * Mark the board with the player's position, and remove that cell 181 | * from the array of empty cells. 182 | * @private 183 | * @param {Object} The player to add 184 | * @param {Array} The player's position 185 | */ 186 | function _addToBoard(player, pos) { 187 | var emptyIndex = -1; 188 | 189 | // Add the player's position to the board 190 | _boardData.board[pos[0]][pos[1]] = player.marker; 191 | 192 | // Remove this position from the array of empty cells 193 | emptyIndex = _boardData.empty.indexOf(pos[0] + ' ' + pos[1]); 194 | _boardData.empty.splice(emptyIndex, 1); 195 | } 196 | 197 | /** 198 | * Print the board 199 | * @private 200 | */ 201 | function _printBoard() { 202 | var divider = '+-----------+'; 203 | 204 | for (var i = 0; i < 3; i += 1) { 205 | console.log(divider); 206 | var row = '|'; 207 | 208 | for (var k = 0; k < 3; k += 1) { 209 | if (_boardData.board[i][k]) 210 | row += ' ' + _boardData.board[i][k] + ' |'; 211 | else 212 | row += ' |'; 213 | } 214 | 215 | console.log(row); 216 | } 217 | 218 | console.log(divider); 219 | } 220 | 221 | /** 222 | * Check the board to see if the player had a winning move 223 | * @private 224 | * @param {Object} The player 225 | */ 226 | function _checkBoard(player) { 227 | var board = _boardData.board 228 | , playerStr = player.marker + player.marker + player.marker 229 | , winner = null 230 | , colStrs = ['', '', ''] 231 | , diagStrs = ['', '']; 232 | 233 | for (var i = 0; i < 3; i += 1) { 234 | 235 | // Check each row for a winner 236 | var rowStr = board[i].join(''); 237 | 238 | if (rowStr == playerStr) { 239 | winner = player; 240 | break; 241 | } 242 | 243 | // Build a string of column values 244 | for (var k = 0; k < 3; k += 1) { 245 | colStrs[k] += board[i][k]; 246 | } 247 | 248 | // Build a string of diagonal values 249 | if (i == 0) { 250 | diagStrs[0] += board[i][0]; 251 | diagStrs[1] += board[i][2]; 252 | } else if (i == 1) { 253 | diagStrs[0] += board[i][1]; 254 | diagStrs[1] += board[i][1]; 255 | } else if (i == 2) { 256 | diagStrs[0] += board[i][2]; 257 | diagStrs[1] += board[i][0]; 258 | } 259 | } 260 | 261 | // Check the column strings for a winner 262 | if (colStrs.indexOf(playerStr) > -1) { 263 | winner = player; 264 | } 265 | 266 | // Check the diagonals for a winner 267 | if (diagStrs.indexOf(playerStr) > -1) { 268 | winner = player; 269 | } 270 | 271 | if (winner) { 272 | console.log(winner.marker + ' wins!'); 273 | return true; 274 | } 275 | 276 | return false; 277 | 278 | } 279 | 280 | 281 | /* 282 | * Public API 283 | * ------------------------------------------------------------------------ 284 | */ 285 | 286 | return { 287 | 288 | /** 289 | * Start the game 290 | * @param {Array} A two element array of player types. Possible values 291 | * are 'human' or 'ai'. 292 | */ 293 | start: function(playerTypes) { 294 | _buildBoard(); 295 | _setupPlayers(playerTypes); 296 | 297 | if (_players[0].marker != 'x') { 298 | // Rotate the players so that 'x' goes first 299 | _rotatePlayers(); 300 | } 301 | 302 | _getPlayerMove(); 303 | }, 304 | 305 | /** 306 | * Return the array of players 307 | */ 308 | getPlayers: function() { 309 | return _players; 310 | } 311 | 312 | } 313 | }; 314 | 315 | ticTacToe = new TicTacToe(readline) 316 | ticTacToe.start(['human', 'ai']); 317 | --------------------------------------------------------------------------------