diff --git a/02week/exercises.js b/02week/exercises.js new file mode 100644 index 000000000..16bcd1f78 --- /dev/null +++ b/02week/exercises.js @@ -0,0 +1,44 @@ +"use strict"; +const cars = ['ford', 'chevy', 'mazda', 'subaru']; +console.log(cars.length); + +const moreCars = ['jeep', 'bmw', 'smart', 'honda']; + +let totalCars = cars.concat(moreCars); + +console.log(moreCars.indexOf('honda')); + +const stringOfCars = totalCars.join(', '); +console.log(stringOfCars); + +totalCars = stringOfCars.split(', '); + +let carsInReverse = totalCars.reverse(); +console.log(carsInReverse); + +carsInReverse.sort(); +console.log(carsInReverse); + +alert(carsInReverse.indexOf('benz')); + +const removedCars = carsInReverse.slice(3, 5); + +carsInReverse.splice(1, 2, 'ford', 'honda'); +console.log(carsInReverse); + +carsInReverse.push('bmw', 'chevy'); + +const deletedItem = carsInReverse.pop(); +console.log(deletedItem); + +const shiftedCar = carsInReverse.shift(); +console.log(shiftedCar); + +carsInReverse.unshift('tesla'); +console.log(carsInReverse); + +const numbers = [23, 45, 0, 2]; +numbers.forEach((item, index) => { + number[index] item + 2; +}); +console.log(numbers); diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 1abf5b900..9b2d40447 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -2,6 +2,7 @@ const assert = require('assert'); const readline = require('readline'); +// https://nodejs.org/api/readline.html const rl = readline.createInterface({ input: process.stdin, output: process.stdout @@ -13,6 +14,7 @@ let board = [ ]; let playerTurn = 'X'; +let moveCount = 0; function printBoard() { console.log(' 0 1 2'); @@ -25,22 +27,143 @@ function printBoard() { function horizontalWin() { // Your code here + // in row, column format these combinations of matching x's or o's will return a win + // 00 01 02 + // 10 11 12 + // 20 21 22 + if ((board[0][0] === board[0][1] && board[0][1] === board[0][2]) || (board[1][0] === board[1][1] && board[1][1] === board[1][2]) || (board[2][0] === board[2][1] + && board[2][1] === board[2][2])) { + return true; + } } function verticalWin() { // Your code here + // in row, column format these combinations of matching x's or o's will return a win + // 00 10 20 + // 01 11 21 + // 02 12 22 + if ((board[0][0] === board[1][0] && board[1][0] === board[2][0]) || (board[0][1] === board[1][1] && board[1][1] === board[2][1]) || (board[0][2] === board[1][2] + && board[1][2] === board[2][2])) { + return true; + } } function diagonalWin() { // Your code here + // in row, column format these combinations of matching x's or o's will return a win + // 00 11 22 + // 02 11 20 + if ((board[0][0] === board[1][1] && board[1][1] === board[2][2]) || (board[0][2] === board[1][1] && board[1][1] === board[2][0])) { + return true; + } } -function checkForWin() { +// const winningCombinations = horizontalWin() || verticalWin() || diagonalWin(); + +// winningCombinations = (row, column) => { +// [ +// // horizontal wins +// [[0,0], [0,1], [0,2]], [[1,0], [1,1], [1,2]], [[2,0], [2,1], [2,2]], +// // verticals wins +// [[0,0], [1,0], [2,0]], [[0,1], [1,1], [2,1]], [[0,2], [1,2], [2,2]], +// // diagonal wins +// [[0,0], [1,1], [2,2]], [[0,2], [1,1], [2,0]]; +// ]; +// } + + + +const checkForWin = () => { // Your code here + // if in any of the above combinations, there are either all x's or all o's then the respective player wins + // on each click, if horizontalWin or verticalWin or diagonalWin - gameover and ${} player wins. If no win, then alternate player. + + // for (var i = 0; i < winningCombinations.length; i++) { + // let i, j + // let count = 0; + // for (let j = 0; j < winningCombinations[i].length; j++) { + // if (board[winningCombinations[i][j]] === playerTurn) { + // count++; + // }if (count === 3) { + // return true; + // } + // } + // return false; + // } + + if (moveCount === 9) { + console.log("It's a tie!"); + printBoard(); + // reset(); + } else if (horizontalWin() || verticalWin() || diagonalWin()) { + console.log(`Player ${playerTurn} Wins!`) + printBoard(); + // reset(); + } } -function ticTacToe(row, column) { - // Your code here +const ticTacToe = (row, column) => { + // the playerturn will start at x but will alternate between xs and os onclicks, and for each click it will checkForWin. + // if win, game winner will be announced and board will reset. + // call printBoard after every turn (push playerTurn to array) + + checkForWin(); + + + // if ((row === 0 || 1 || 2) && (column === 0 || 1 || 2)) { + + // } else { + // console.log("Invalid move, please try again!") + // } + + // board[row][column] = playerTurn; + // if (moveCount > 0){ + // checkForWin(); + // } + // moveCount ++; + // playerTurn = (playerTurn === "X" ? "O" : "X"); + // } + + if (board[row][column] === ' ') { + board[row][column] = playerTurn; + // board[row].splice(column, 1, playerTurn); + } + + + // const validValue = (myIndex) => { + // const valuesArr = [0,1,2]; + // return valuesArr.some(validIndex => myIndex == validIndex); + // } + // + // if (validValue(row) && validValue(column)) { + // if (!board[row][column].trim() ) { + // board[row][column] = playerTurn; + // + // if (!checkForWin()) { + // if (playerTurn === 'X') { + // playerTurn = 'O'; + // } else { + // playerTurn = 'X'; + // } + // return false; + // } else { + // console.log(`The winner is player ${playerTurn}. Start a new game`); + // return true; + // } + // } else { + // console.log('Please choose another square! That one is taken!'); + // } + // } else { + // console.log('Please enter a valid index. Valid values are 0, 1, 2'); + // } + moveCount++; + + if (playerTurn === 'X') { + playerTurn = 'O'; + } else { + playerTurn = 'X'; + } } function getPrompt() {