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
38 changes: 38 additions & 0 deletions 02week/exercises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
let cars = ['Ford','Lamborghini','Ferrari','Yamaha'];
console.log('Cars array: ' + cars);
let moreCars = ['Hyundai','Mercedes','Toyota','Honda'];
console.log('moreCars array: ' + moreCars);
let totalCars = cars.concat(moreCars);
console.log('totalCars array: ' + totalCars);
console.log('Location of Honda: ' + totalCars.indexOf('Honda'));
console.log('Location of Ford: ' + totalCars.lastIndexOf('Ford'));
let stringOfCars = totalCars.join();
console.log('stringOfCars: ' + stringOfCars);
totalCars = stringOfCars.split(',');
console.log('Reconstituted "totalCars": ' + totalCars);
let carsInReverse = totalCars.reverse();
console.log('carsInReverse: ' + carsInReverse);
console.log('Cars Alphabetized: ' + carsInReverse.sort());
//alert(carsInReverse.indexOf('Ford')); This line of code is copy+pasted from the textbook but generates an error
sliceHonda = carsInReverse.slice(0,1);
sliceFord = carsInReverse.slice(7,8);
removedCars = sliceHonda.concat(sliceFord);
console.log('RemovedCars: ' + removedCars);
console.log('Sliced carsInReverse: ' + carsInReverse);
carsInReverse.splice(1,2,removedCars[0],removedCars[1]);
console.log('Spliced carsInReverse: ' + carsInReverse);
carsInReverse.push('Toyota');
carsInReverse.push('Mercedes');
console.log('Pushed carsInReverse: ' + carsInReverse);
let popped = carsInReverse.pop();
console.log('Popped: ' + popped);
let shifted = carsInReverse.shift();
console.log('Shifted: ' + shifted);
carsInReverse.unshift('Chevy');
console.log('Final carsInReverse: ' + carsInReverse);
let numbers = [23,45,0,2];
numbers.forEach(function(element,index) {
element = element + 2;
numbers.splice(index,1,element);
});
console.log('Numbers: ' + numbers);
56 changes: 46 additions & 10 deletions 03week/ticTacToe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ let board = [
[' ', ' ', ' ']
];

let blankBoard = [ //blankBoard is for the purposes of resetting after a game
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
];

let playerTurn = 'X';

function printBoard() {
Expand All @@ -24,23 +30,55 @@ function printBoard() {
}

function horizontalWin() {
// Your code here
if (((board[0][0]!=' ')&&((board[0][0]===board[0][1])&&(board[0][0]===board[0][2]))) || //top row
((board[1][0]!=' ')&&((board[1][0]===board[1][1])&&(board[1][0]===board[1][2]))) || //middle row
((board[2][0]!=' ')&&((board[2][0]===board[2][1])&&(board[2][0]===board[2][2])))){ //bottom row
return true
}
}

function verticalWin() {
// Your code here
if (((board[0][0]!=' ')&&((board[0][0]===board[1][0])&&(board[0][0]===board[2][0]))) || //left column
((board[0][1]!=' ')&&((board[0][1]===board[1][1])&&(board[0][1]===board[2][1]))) || //middle column
((board[0][2]!=' ')&&((board[0][2]===board[1][2])&&(board[0][2]===board[2][2])))){ //right column
return true
}
}

function diagonalWin() {
// Your code here
if (((board[0][0]!=' ')&&((board[0][0]===board[1][1])&&(board[0][0]===board[2][2]))) ||
((board[2][0]!=' ')&&((board[2][0]===board[1][1])&&(board[2][0]===board[0][2])))){
return true
}
}

function checkForWin() {
// Your code here
if (horizontalWin()||verticalWin()||diagonalWin()){
console.log("********************");
console.log("***PLAYER "+playerTurn+" WINS!***");
console.log("********************");
board = blankBoard;
return true;
}
}

function ticTacToe(row, column) {
// Your code here
//checks for valid input
if (((row==='0')||(row==='1')||(row==='2'))&&((column==='0')||(column==='1')||(column==='2'))){
if ((playerTurn==='X')&&(board[row][column]===' ')){
board[row][column]='X';
checkForWin();
playerTurn='O'; //change player turn
} else if ((playerTurn==='O')&&(board[row][column]===' ')){
board[row][column]='O';
checkForWin();
playerTurn='X'; //change player turn
} else if (board[row][column]!=' ') {
console.log("***That space already has a symbol in it. Try again.***");
}
}else { //if invalid input
console.log("***That input is invalid. Try again.***");
}
}

function getPrompt() {
Expand All @@ -55,19 +93,17 @@ function getPrompt() {

}



// Tests
// first two tests had errors: the inputs for ticTacToe() need to be strings

if (typeof describe === 'function') {

describe('#ticTacToe()', () => {
it('should place mark on the board', () => {
ticTacToe(1, 1);
ticTacToe('1', '1');
assert.deepEqual(board, [ [' ', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]);
});
it('should alternate between players', () => {
ticTacToe(0, 0);
ticTacToe('0', '0');
assert.deepEqual(board, [ ['O', ' ', ' '], [' ', 'X', ' '], [' ', ' ', ' '] ]);
});
it('should check for vertical wins', () => {
Expand Down