Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 71 additions & 7 deletions 03week/ticTacToe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,105 @@ 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(' | '));
console.log(' ---------');
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();
}
});
});

Expand Down
71 changes: 69 additions & 2 deletions 04week/functional-javascript/helloWorld.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

//