From c15c484ec4bd07b5dd7283d7da880a98aaeefb94 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Tue, 22 Jan 2019 22:25:10 -0600 Subject: [PATCH 1/5] did exercises.js homework --- 03week/exercises.js | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 03week/exercises.js diff --git a/03week/exercises.js b/03week/exercises.js new file mode 100644 index 000000000..c33954a42 --- /dev/null +++ b/03week/exercises.js @@ -0,0 +1,58 @@ +'use strict' + +// 1. Create array 'cars' with 4 different types of cars & console.log: +let cars = ['Ford', 'Lotus', 'Renault', 'Peugeot']; +console.log(cars); + +//2. Create array 'morecars' with 4 more types of cars and concat with cars: +let moreCars = ['Pagani', 'Porsche', 'Audi', 'Honda']; +let totalCars = cars.concat(moreCars); + +//3. console.log the indexOf 'Honda' and lastIndexOf 'Ford': +console.log(totalCars.indexOf('Honda')); +console.log(totalCars.lastIndexOf('Ford')); + +//4. Use join method to convert the array totalCars to a string: +let stringOfCars = totalCars.join(' '); +console.log(stringOfCars); + +//5. Use split method to convert stringOfCars back to an array: +totalCars = stringOfCars.split(' '); + +//6. Use reverse method to create an array carsInReverse from totalCars: +let carsInReverse = totalCars.reverse(); + +//7: Use sort method to put carsInReverse in alphabetical order: +carsInReverse.sort(); +// prediction: 'Audi' is in index 0: +console.log(carsInReverse.indexOf('Audi')); + +//8. Use slice method to remove Ford and Honda from carsInReverse, +// and move them into a new array removedCars: +let removedCars = carsInReverse.slice(1, 3); +console.log(removedCars); + +//9. Use splice method to remove 2nd and 3rd items in carsInReverse & replace them with 'Ford' and 'Honda' (which is weird because the 2nd and 3rd items are already Ford and Honda...): +carsInReverse.splice(1, 2, 'Ford', 'Honda'); +console.log(carsInReverse); + +//10. Use push method to add the types of cars removed using splice method to carsInReverse: +carsInReverse.push('Ford', 'Honda'); +console.log(carsInReverse); + +//11. Use pop method to remove and console.log the last item in carsInReverse: +console.log(carsInReverse.pop()); + +//12. Use shift method to remove and console.log the first item in carsInReverse: +console.log(carsInReverse.shift()); + +//13. Use the unshift method to add a new type of car to the beginning of carsInReverse: +carsInReverse.unshift('Volkswagen'); +console.log(carsInReverse); + +//14. Create an array called numbers with the following items: 23, 45, 0, 2, write code that will add 2 to each item in the array numbers. +let numbers = [ 23, 45, 0, 2 ]; +for(let x in numbers){ + numbers[x] += 2; + console.log(numbers[x]); +} From 98f7fa41e1729a5fdbadadd6ec3655082c112f30 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 23 Jan 2019 03:20:38 -0600 Subject: [PATCH 2/5] built tic tac toe, need to finish vert win state, 5/6 tests passing --- 03week/ticTacToe.js | 97 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 7 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..640c869b6 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -23,24 +23,109 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } +let reset = () => { + board = [ + [' ', ' ', ' '], + [' ', ' ', ' '], + [' ', ' ', ' '] + ]; + playerTurn = 'X'; +} + +let playerString = playerTurn + playerTurn + playerTurn; function horizontalWin() { - // Your code here + // using for/in with string comparators and joining the row arrays together: + for( let i in board ) { + let rowString = board[i].join(''); + if(rowString == playerString) { + return true; + } + else return false; + } + + // alternative method for row win state using .every(): + // if( + // (board[0].every(x => x == playerTurn)) || + // (board[1].every(x => x == playerTurn)) || + // (board[2].every(x => x == playerTurn)) + // ) { + // return true; + // } + // else return false; } function verticalWin() { // Your code here } -function diagonalWin() { - // Your code here +let diagonalWin = () => { + if( board[1][1] != playerTurn ) { + return false; + } + else if((board[0][0] == playerTurn && board[2][2] == playerTurn) || + (board[0][2] == playerTurn && board[2][0] == playerTurn)) { + return true; + } + else return false; } function checkForWin() { - // Your code here + if( horizontalWin() || verticalWin() || diagonalWin() ) { + return true; + } + else return false; } +// create a function to return true if the choices are valid (note: added number cases cuz prewritten test wouldn't work with strings): +let choiceChecker = (row, column) => { + switch(row){ + case '0': + case '1': + case '2': + case 0: + case 1: + case 2: + break; + default: + return false; + } + switch(column){ + case '0': + case '1': + case '2': + case 0: + case 1: + case 2: + return true; + default: + return false; + } +} + +// create function to flip turn (just to make it easier to read in the ticTacToe function): +let flipTurn = () => { playerTurn == 'X' ? playerTurn = 'O' : playerTurn = 'X'; } + function ticTacToe(row, column) { - // Your code here + // Check if turn is valid: + let isValid = choiceChecker(row, column); + // If turn is not valid, say so: + if(!isValid) { + console.log('Try again, enter a valid row and column!'); + } + // else if chosen space is already occupied, say so: + else if(board[row][column] != ' ') { + console.log('That space is already taken, try again!'); + } + // or else put the current turn ('X' or 'O') in the array space represented by the inputs: + else { + board[row][column] = playerTurn; + // check if there's a win and then flip the turn: + if(checkForWin()){ + console.log(`${playerTurn} wins!`); + reset(); + } + else flipTurn(); + } } function getPrompt() { @@ -55,8 +140,6 @@ function getPrompt() { } - - // Tests if (typeof describe === 'function') { From b9fed6a92085f0e42a333d2c074bb0d2c482ab98 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 23 Jan 2019 03:38:15 -0600 Subject: [PATCH 3/5] made vert win checker, kind of brute forced but passes tests. will attempt to come up with more elegant solution --- 03week/ticTacToe.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 640c869b6..89227f073 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -42,7 +42,6 @@ function horizontalWin() { } else return false; } - // alternative method for row win state using .every(): // if( // (board[0].every(x => x == playerTurn)) || @@ -55,7 +54,15 @@ function horizontalWin() { } function verticalWin() { - // Your code here + // brute force method that i'm not super happy with: + if( + (board[0][0] == playerTurn && board[1][0] == playerTurn && board[2][0] == playerTurn) || + (board[0][1] == playerTurn && board[1][1] == playerTurn && board[2][1] == playerTurn) || + (board[0][2] == playerTurn && board[1][2] == playerTurn && board[2][2] == playerTurn) + ) { + return true; + } + else return false; } let diagonalWin = () => { From 23bccf6a5b747c85e3bfcad7fcfdea4ead882295 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 23 Jan 2019 17:54:52 -0600 Subject: [PATCH 4/5] added statement to check for tie game. added more comments --- 03week/ticTacToe.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 89227f073..b44c33f5d 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -23,6 +23,7 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } +// define reset function to reset board after a game: let reset = () => { board = [ [' ', ' ', ' '], @@ -30,13 +31,17 @@ let reset = () => { [' ', ' ', ' '] ]; playerTurn = 'X'; + turnCount = 1; } -let playerString = playerTurn + playerTurn + playerTurn; function horizontalWin() { - // using for/in with string comparators and joining the row arrays together: + // make a string depending on the playerTurn, 'XXX' or 'OOO' to compare against: + let playerString = playerTurn + playerTurn + playerTurn; + // method using for/in with string comparators and joining the row arrays together: for( let i in board ) { + // join each subarray into a string with no spaces: let rowString = board[i].join(''); + // compare rowString against playerString to detect horizontal win: if(rowString == playerString) { return true; } @@ -54,7 +59,7 @@ function horizontalWin() { } function verticalWin() { - // brute force method that i'm not super happy with: + // brute force method that i'm not super happy with, checking each possible win iteration using && and || in an if statement: if( (board[0][0] == playerTurn && board[1][0] == playerTurn && board[2][0] == playerTurn) || (board[0][1] == playerTurn && board[1][1] == playerTurn && board[2][1] == playerTurn) || @@ -66,9 +71,11 @@ function verticalWin() { } let diagonalWin = () => { + // check middle square first, because it can't be a diagonal win if the middle square isn't occupied: if( board[1][1] != playerTurn ) { return false; } + // else brute force check the remaining spaces required to make a diagonal win: else if((board[0][0] == playerTurn && board[2][2] == playerTurn) || (board[0][2] == playerTurn && board[2][0] == playerTurn)) { return true; @@ -76,6 +83,7 @@ let diagonalWin = () => { else return false; } +// make checkForWin return true if any of the win state checks are true: function checkForWin() { if( horizontalWin() || verticalWin() || diagonalWin() ) { return true; @@ -112,6 +120,9 @@ let choiceChecker = (row, column) => { // create function to flip turn (just to make it easier to read in the ticTacToe function): let flipTurn = () => { playerTurn == 'X' ? playerTurn = 'O' : playerTurn = 'X'; } +// create turn counter variable as storage to help determine a tie/cat's game: +let turnCount = 1; + function ticTacToe(row, column) { // Check if turn is valid: let isValid = choiceChecker(row, column); @@ -126,12 +137,21 @@ function ticTacToe(row, column) { // or else put the current turn ('X' or 'O') in the array space represented by the inputs: else { board[row][column] = playerTurn; - // check if there's a win and then flip the turn: + // check if there's a win, if so log it and reset the board: if(checkForWin()){ console.log(`${playerTurn} wins!`); reset(); } - else flipTurn(); + // or else if there's no winner and all the spaces are full, log tie game and reset: + else if( !checkForWin() && turnCount == 9 ) { + console.log("Cat's game! Tie!"); + reset(); + } + // or else flip the turn and add 1 to the turn counter: + else { + flipTurn(); + turnCount++; + } } } From d88e376cd31d2ba34a84378a458e90a7febdbb7f Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Thu, 24 Jan 2019 03:51:00 -0600 Subject: [PATCH 5/5] updated to arrow functions, changed horiz win check method. few minor other things --- 03week/ticTacToe.js | 71 ++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index b44c33f5d..5be3061bf 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -6,6 +6,7 @@ const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); + let board = [ [' ', ' ', ' '], [' ', ' ', ' '], @@ -14,7 +15,7 @@ let board = [ let playerTurn = 'X'; -function printBoard() { +let printBoard = () => { console.log(' 0 1 2'); console.log('0 ' + board[0].join(' | ')); console.log(' ---------'); @@ -34,31 +35,33 @@ let reset = () => { turnCount = 1; } -function horizontalWin() { - // make a string depending on the playerTurn, 'XXX' or 'OOO' to compare against: - let playerString = playerTurn + playerTurn + playerTurn; - // method using for/in with string comparators and joining the row arrays together: - for( let i in board ) { - // join each subarray into a string with no spaces: - let rowString = board[i].join(''); - // compare rowString against playerString to detect horizontal win: - if(rowString == playerString) { - return true; - } - else return false; +// make a string depending on the playerTurn, e.g. 'XXX' or 'OOO' to compare against: +let playerString = playerTurn + playerTurn + playerTurn; + +let horizontalWin = () => { + // method for row win state using .every(): + if( + (board[0].every(x => x == playerTurn)) || + (board[1].every(x => x == playerTurn)) || + (board[2].every(x => x == playerTurn)) + ) { + return true; } - // alternative method for row win state using .every(): - // if( - // (board[0].every(x => x == playerTurn)) || - // (board[1].every(x => x == playerTurn)) || - // (board[2].every(x => x == playerTurn)) - // ) { - // return true; + else return false; + + // method using for/in with string comparators and joining the row arrays together (not working as intended): + // for( let i in board ) { + // // join each subarray into a string with no spaces: + // let rowString = board[i].join(''); + // // compare rowString against playerString to detect horizontal win: + // if( rowString == playerString ) { + // return true; + // } + // else return false; // } - // else return false; } -function verticalWin() { +let verticalWin = () => { // brute force method that i'm not super happy with, checking each possible win iteration using && and || in an if statement: if( (board[0][0] == playerTurn && board[1][0] == playerTurn && board[2][0] == playerTurn) || @@ -84,7 +87,7 @@ let diagonalWin = () => { } // make checkForWin return true if any of the win state checks are true: -function checkForWin() { +let checkForWin = () => { if( horizontalWin() || verticalWin() || diagonalWin() ) { return true; } @@ -92,7 +95,7 @@ function checkForWin() { } // create a function to return true if the choices are valid (note: added number cases cuz prewritten test wouldn't work with strings): -let choiceChecker = (row, column) => { +let choiceChecker = ( row, column ) => { switch(row){ case '0': case '1': @@ -118,33 +121,35 @@ let choiceChecker = (row, column) => { } // create function to flip turn (just to make it easier to read in the ticTacToe function): -let flipTurn = () => { playerTurn == 'X' ? playerTurn = 'O' : playerTurn = 'X'; } +let flipTurn = () => { + playerTurn == 'X' ? playerTurn = 'O' : playerTurn = 'X'; +} // create turn counter variable as storage to help determine a tie/cat's game: let turnCount = 1; -function ticTacToe(row, column) { +let ticTacToe = ( row, column ) => { // Check if turn is valid: let isValid = choiceChecker(row, column); // If turn is not valid, say so: - if(!isValid) { + if( !isValid ) { console.log('Try again, enter a valid row and column!'); } // else if chosen space is already occupied, say so: - else if(board[row][column] != ' ') { + else if( board[row][column] != ' ' ) { console.log('That space is already taken, try again!'); } // or else put the current turn ('X' or 'O') in the array space represented by the inputs: else { board[row][column] = playerTurn; // check if there's a win, if so log it and reset the board: - if(checkForWin()){ - console.log(`${playerTurn} wins!`); + if( checkForWin() ) { + console.log( `${playerTurn} wins!` ); reset(); } // or else if there's no winner and all the spaces are full, log tie game and reset: else if( !checkForWin() && turnCount == 9 ) { - console.log("Cat's game! Tie!"); + console.log( "Cat's game! Tie!" ); reset(); } // or else flip the turn and add 1 to the turn counter: @@ -155,9 +160,9 @@ function ticTacToe(row, column) { } } -function getPrompt() { +let getPrompt = () => { printBoard(); - console.log("It's Player " + playerTurn + "'s turn."); + console.log( `It's player ${playerTurn}'s turn.` ); rl.question('row: ', (row) => { rl.question('column: ', (column) => { ticTacToe(row, column);