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]); +} diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..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(' ---------'); @@ -23,29 +24,145 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } -function horizontalWin() { - // Your code here +// define reset function to reset board after a game: +let reset = () => { + board = [ + [' ', ' ', ' '], + [' ', ' ', ' '], + [' ', ' ', ' '] + ]; + playerTurn = 'X'; + turnCount = 1; +} + +// 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; + } + 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; + // } +} + +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) || + (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; } -function verticalWin() { - // Your code here +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; + } + else return false; } -function diagonalWin() { - // Your code here +// make checkForWin return true if any of the win state checks are true: +let checkForWin = () => { + if( horizontalWin() || verticalWin() || diagonalWin() ) { + return true; + } + else return false; } -function checkForWin() { - // Your code here +// 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; + } } -function ticTacToe(row, column) { - // Your code here +// 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 getPrompt() { +// create turn counter variable as storage to help determine a tie/cat's game: +let turnCount = 1; + +let ticTacToe = ( row, column ) => { + // 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, if so log it and reset the board: + 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!" ); + reset(); + } + // or else flip the turn and add 1 to the turn counter: + else { + flipTurn(); + turnCount++; + } + } +} + +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); @@ -55,8 +172,6 @@ function getPrompt() { } - - // Tests if (typeof describe === 'function') {