diff --git a/03week/ticTacToe.js b/03week/ticTacToe.js index 1abf5b900..950c9874a 100644 --- a/03week/ticTacToe.js +++ b/03week/ticTacToe.js @@ -15,7 +15,7 @@ let board = [ let playerTurn = 'X'; function printBoard() { - console.log(' 0 1 2'); + console.log('\n 0 1 2'); console.log('0 ' + board[0].join(' | ')); console.log(' ---------'); console.log('1 ' + board[1].join(' | ')); @@ -23,33 +23,97 @@ function printBoard() { console.log('2 ' + board[2].join(' | ')); } + +//Check for horizontal win function horizontalWin() { - // Your code here + //loop through each row of the board and check if they have three Xs or Os. + //wanted to use forEach but couldn't figure out how to console log the correct winner + //console.log which player won + for (var i = 0; i < 3; i++) { + if (board[i].join('') === 'XXX' || board[i].join('') === 'OOO') { + console.log(`Player ${board[i][0]} wins!`); + return true; + } + } } +//Check for vertical win function verticalWin() { - // Your code here + //create a variable vertCheck + // + let vertCheck; + for (var i = 0; i < 3; i++) { + //Reset vertCheck to blank + vertCheck = ''; + for (var x = 0; x < 3; x++) { + vertCheck = vertCheck + board[x][i]; + } + if (vertCheck === 'XXX' || vertCheck === 'OOO') { + console.log(`Player ${vertCheck.charAt(0)} wins!`); + return true; + } + } } +//Check for diagonal win function diagonalWin() { - // Your code here + // + //Set upLeft = to string of upper left to lower right + let upLeft = board[0][0] + board[1][1] + board[2][2]; + //Set upRight = to string of upper right to lower left + let upRight = board[0][2] + board[1][1] + board[2][0]; + //Check if upLeft or upRight are xxx or OOO + if (upLeft === 'XXX' || upLeft === 'OOO' ||upRight === 'XXX' || upRight === 'OOO') { + console.log(`\nPlayer ${board[1][1]} wins!`); + return true; + } } + function checkForWin() { - // Your code here + //checkForWin checks if any of the other win states are true + //if any of them are true, returns true + if ( + horizontalWin() === true || + verticalWin() === true || + diagonalWin() === true + ) { + return true; + } } +function isLegal(row, column) { + //isLegal checks if row and column equal X or O + //if row or column !=== X or 0, rerun getPrompt + //if row or column does ==== X or O, return true + if (((row === 0) || (row === 1) || (row === 2)) && ((column === 0) || (column === 1) || (column === 2))) { + true; + } else if (((row !== 0) || (row !== 1) || (row !== 2)) && ((column !== 0) || (column !== 1) || (column !== 2))) { + console.log('Please chose either 0, 1, or 2.') + return getPrompt(); + } +}; + +//Main TTT function function ticTacToe(row, column) { - // Your code here + if (board[row][column] === ' ') { + board[row][column] = playerTurn; + playerTurn = (playerTurn==='X'?'O':'X'); + checkForWin(); + } else { + console.log('\nInvalid location!'); + } } function getPrompt() { printBoard(); - console.log("It's Player " + playerTurn + "'s turn."); + console.log("\nIt's player " + playerTurn + "'s turn."); rl.question('row: ', (row) => { rl.question('column: ', (column) => { + if (isLegal(row, column) === true) { ticTacToe(row, column); getPrompt(); + } }); }); diff --git a/04week/functional-javascript/helloWorld.js b/04week/functional-javascript/helloWorld.js index adb078759..38ddc4bc9 100644 --- a/04week/functional-javascript/helloWorld.js +++ b/04week/functional-javascript/helloWorld.js @@ -1,5 +1,72 @@ +'use strict' + +//# 1 function upperCaser(input) { - return input.toUpperCase(); -} + return input.toUpperCase(); +}; module.exports = upperCaser + +//# 2 +//Looked up how to make a recusive function. Plugged the argument names into function +function repeat(operation, num) { + if (num === 0) { + return operation +} +return repeat(num — 1, num * operation) +} + } + +// Do not remove the line below +module.exports = repeat + +//# 3 +var numbers = [1, 5, 10, 15]; +var doubles = numbers.map(function(x) { + return x * 2; +}; + +//# 4 +function getShortMessages(messages) { + // SOLUTION GOES HERE + } + + module.exports = getShortMessages + +//# 5 + +//goodUsers, a list of valid users +var goodUsers = [ + { id: 1 }, + { id: 2 }, + { id: 3 } +] + +// `checkUsersValid` is the function you'll define +var testAllValid = checkUsersValid(goodUsers) + +const checkUsersValid = (goodUsers) => { + if (!testAllValid.same(goodusers)) { + console.log('None correct.'); + } else if { + (testAllValid.same(goodusers) === true { + testAllValid.every(goodusers) { + if true { + console.log ('All correct!'); + } else { + console.log('At least one user wrong.'); + } + } + } + } +}; + +//Given an Array of strings, use Array#reduce to create an object that contains the number of times each string occured in the array. Return the object directly (no need to console.log). + +function countWords(inputWords) { + var result = numbers.reduce(function(accumulator, currentValue) { + return accumulator + currentValue; + } +} + +//