From c66234a89a8d06a622f0a8291c02bacd286d3b57 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Tue, 25 Jul 2017 18:40:22 -0500 Subject: [PATCH 1/2] Arrays --- 02week/arrayspj.js | 65 +++++++++++++++++++++++++++++++++++++++++++++ 02week/ticTacToe.js | 32 +++++++++++++++++----- 2 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 02week/arrayspj.js diff --git a/02week/arrayspj.js b/02week/arrayspj.js new file mode 100644 index 000000000..cfc27bf62 --- /dev/null +++ b/02week/arrayspj.js @@ -0,0 +1,65 @@ +'use strict' + +//length +let cars = ['Ford','Toyota','Cadillac','Hyundai']; +console.log(cars.length); + +//concat +let moreCars = ['Aston Martin','Bugatti','Bentley','Honda']; +let totalCars = cars.concat(moreCars); +console.log(totalCars); + +//indexOf +console.log(totalCars.indexOf("Honda")); + +//lastIndexOf +console.log(totalCars.lastIndexOf("Ford")); + +//join +let stringOfCars = totalCars.join(','); +console.log(stringOfCars); + +//split +totalCars = stringOfCars.split(','); +console.log(totalCars); + +//reverse +let carsInReverse = totalCars.reverse(); +console.log(carsInReverse); + +//sort +carsInReverse.sort(); +alert(carsInReverse.indexOf('Aston Martin')); +console.log(carsInReverse); + +//slice +let removedCars = carsInReverse.slice(4, 6); +console.log(removedCars); + +//splice +carsInReverse.splice(1, 1, 'Ford'); +carsInReverse.splice(2, 1, 'Honda'); +console.log(carsInReverse); + +//push +carsInReverse.push('Ford', 'Honda'); +console.log(carsInReverse); + +//pop +carsInReverse.pop(); +console.log(carsInReverse); + +//shift +carsInReverse.shift(); +console.log(carsInReverse); + +//unshift +carsInReverse.unshift("Subaru"); +console.log(carsInReverse); + +//forEach +var numbers = [23, 45, 0, 2]; +function addTwo(item) { + console.log(item + 2); +} +numbers.forEach(addTwo) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 1abf5b900..c3ed51eb2 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -1,5 +1,10 @@ 'use strict'; +//Horizontal Win [0,1,2], [3,4,5], [6,7,8] +// Vertical Win: [0,3,6], [1,4,7], [2,5,8] +// Diagonal Win: [0,4,8], [0,2,6] +// Only way to win is if the total is 0 or if the total is 3. X are worth 1 point and O are worth 0 points + const assert = require('assert'); const readline = require('readline'); const rl = readline.createInterface({ @@ -23,17 +28,32 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } +// Horizontal Win: [0,1,2], [3,4,5], [6,7,8] function horizontalWin() { - // Your code here -} + if ('a win in a row') { + [ { x: 0, o: 6 }, { x: 1, o: 7 }, { x: 2, o: 8 }] || [ { x: 3, o: 0 }, { x: 4, o: 1 }, { x: 5, o: 2 }] || [ { x: 6, o: 3 }, { x: 7, o: 4 }, { x: 8, o: 5 }] { + + }); + return ('player " " wins!'); + }); +// Vertical Win: [0,3,6], [1,4,7], [2,5,8] function verticalWin() { - // Your code here -} + if ('a win in a column') { + [ { x: 0, o: 1 }, { x: 3, o: 4 }, { x: 6, o: 7 }] || [ { x: 2, o: 0 }, { x: 5, o: 3 }, { x: 8, o: 6 }] || [ { x: 1, o: 2 }, { x: 4, o: 5 }, { x: 7, o: 8 }] { + + }); + return ('player " " wins!'); + }); +// Diagonal Win: [0,4,8], [2,4,6] function diagonalWin() { - // Your code here -} + if ('a win diagonally') { + [ { x: 0, o: 2 }, { x: 4, o: 4 }, { x: 8, o: 6 }] || [ { x: 2, o: 0 }, { x: 4, o: 4 }, { x: 6, o: 8 }] + + }); + return ('player " " wins!'); +}); function checkForWin() { // Your code here From ef0366ba3b0069e578eaf3a25ceb3432dc3cfe7f Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Sun, 30 Jul 2017 21:14:21 -0500 Subject: [PATCH 2/2] tictactoe --- 02week/ticTacToe.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index c3ed51eb2..e31091366 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -1,5 +1,16 @@ 'use strict'; + +//if no, push that player marker to that index +//make a copy to the board +//const updatedBoard = [...board]; +//slice the row, store to const, (not splice) +//const updatedRow = board.slice(row, row+1) [0]; +//console.log(updatedRow, 'Row') + +/*Check it marker exists at row/columns +If no wins, switch player, display the updated boardIf any wins come back return the player winner, clear the board*/ + //Horizontal Win [0,1,2], [3,4,5], [6,7,8] // Vertical Win: [0,3,6], [1,4,7], [2,5,8] // Diagonal Win: [0,4,8], [0,2,6] @@ -31,29 +42,28 @@ function printBoard() { // Horizontal Win: [0,1,2], [3,4,5], [6,7,8] function horizontalWin() { if ('a win in a row') { - [ { x: 0, o: 6 }, { x: 1, o: 7 }, { x: 2, o: 8 }] || [ { x: 3, o: 0 }, { x: 4, o: 1 }, { x: 5, o: 2 }] || [ { x: 6, o: 3 }, { x: 7, o: 4 }, { x: 8, o: 5 }] { - - }); - return ('player " " wins!'); - }); + [ { x: 0, o: 6 }, { x: 1, o: 7 }, { x: 2, o: 8 }] || [ { x: 3, o: 0 }, {x: 4, o: 1 }, { x: 5, o: 2 }] || [ { x: 6, o: 3 }, { x: 7, o: 4 }, { x: 8, o: 5 }] + }; + return ('player " " wins!'); +}; // Vertical Win: [0,3,6], [1,4,7], [2,5,8] function verticalWin() { if ('a win in a column') { - [ { x: 0, o: 1 }, { x: 3, o: 4 }, { x: 6, o: 7 }] || [ { x: 2, o: 0 }, { x: 5, o: 3 }, { x: 8, o: 6 }] || [ { x: 1, o: 2 }, { x: 4, o: 5 }, { x: 7, o: 8 }] { + [ { x: 0, o: 1 }, { x: 3, o: 4 }, { x: 6, o: 7 }] || [ { x: 2, o: 0 }, { x: 5, o: 3 }, { x: 8, o: 6 }] || [ { x: 1, o: 2 }, { x: 4, o: 5 }, { x: 7, o: 8 }] - }); - return ('player " " wins!'); - }); + }; + return ('player " " wins!'); +}; // Diagonal Win: [0,4,8], [2,4,6] function diagonalWin() { if ('a win diagonally') { [ { x: 0, o: 2 }, { x: 4, o: 4 }, { x: 8, o: 6 }] || [ { x: 2, o: 0 }, { x: 4, o: 4 }, { x: 6, o: 8 }] - }); + }; return ('player " " wins!'); -}); +}; function checkForWin() { // Your code here