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
108 changes: 108 additions & 0 deletions 02week/project1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
'use strict';

console.log('Project 1');
console.log('');
console.log('');

// Test 1 - Print out array length
console.log('Test 1 - array length');
let cars = ['Ford','Chevy','Chrysler','Cadillac'];
console.log(cars.length);
console.log('');

// Test 2 - Use concat method to combine 2 arrays.
console.log('Test 2 - using concat');
let moreCars = ['BMW','Mercedes','Toyota','Honda'];
let totalCars = cars.concat(moreCars);
console.log(totalCars);
console.log('');

// Test 3 - Using indexOf and lastIndexOf
console.log('Test 3 - using indexOf and lastIndexOf');
console.log(`Index for Honda is ${totalCars.indexOf('Honda')}`);
console.log(`Index for Ford is ${totalCars.lastIndexOf('Ford')}`);
console.log('');

// Test 4 - Using join
console.log('Test 4 - using join');
const stringOfCars = totalCars.join(', ');
console.log(stringOfCars);
console.log('');

// Test 5 - Using split
console.log('Test 5 - using split');
totalCars = stringOfCars.split(', ');
console.log(totalCars);
console.log('');

// Test 6 - Using reverse
console.log('Test 6 - using reverse');
const carsInReverse = totalCars.reverse();
console.log(carsInReverse);
console.log('');


// Test 7 - Using sort
console.log('Test 7 - using sort');
console.log(carsInReverse.sort());
// alert(`Does BMW have first position in array? BMW index = ${carsInReverse.indexOf('BMW')}`);
console.log(`Does BMW have first position in array? BMW index = ${carsInReverse.indexOf('BMW')}`);
console.log('');


// Test 8 - Using slice
console.log('Test 8 - Using slice');
const removedCars = carsInReverse.slice(4,-2);
console.log(carsInReverse); // still has the values
console.log(removedCars);
console.log('');


// Test 9 - Using splice
console.log('Test 9 - using splice');
console.log(carsInReverse.splice(1, 2, 'Ford', 'Honda'));
console.log(carsInReverse);
console.log('');


// Test 10 - Using push
console.log('Test 10 - using push');
carsInReverse.push('Cadillac', 'Chevy');
console.log(carsInReverse);
console.log('');


// Test 11 - Using pop
console.log('Test 11 - using pop');
console.log(`The last entry removed: ${carsInReverse.pop()}`);
console.log(`The last car in the array now: ${carsInReverse[carsInReverse.length-1]}`);
console.log('');


// Test 12 - Using shift
console.log('Test 12 - using shift');
console.log(`The first entry removed: ${carsInReverse.shift()}`);
console.log(`The first car in the array now: ${carsInReverse[0]}`);
console.log('');


// Test 13 - Using unshift
console.log('Test 13 - using unshift');
carsInReverse.unshift('Buick');
console.log(carsInReverse);
console.log('');


// Test 14 - Use For Each to add 2 to each element in array.
console.log('Test 14 - using forEach');
function addTwo() {

let numbers = [23, 45, 0, 2];
console.log(numbers);
numbers.forEach((theNum, index) => {
numbers[index] = theNum + 2});
console.log(' adding 2...');
console.log(numbers);

}
addTwo();
69 changes: 60 additions & 9 deletions 02week/ticTacToe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let board = [
];

let playerTurn = 'X';
let turnCount = 0;

function printBoard() {
console.log(' 0 1 2');
Expand All @@ -24,39 +25,89 @@ function printBoard() {
}

function horizontalWin() {
// Your code here
return board[0].every(square => square === playerTurn) || board[1].every(square => square === playerTurn) || board[2].every(square => square === playerTurn);
}

function verticalWin() {
// Your code here
return [board[0][0], board[1][0], board[2][0]].every(square => square === playerTurn) || [board[0][1], board[1][1], board[2][1]].every(square => square === playerTurn) || [board[0][2], board[1][2], board[2][2]].every(square => square === playerTurn);
}

function diagonalWin() {
// Your code here
return [board[0][0], board[1][1], board[2][2]].every(square => square === playerTurn) || [board[0][2], board[1][1], board[2][0]].every(square => square === playerTurn);
Copy link

@pieroapretto pieroapretto Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the .every() JavaScript method here.

nicely done

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate it!

}

function checkForWin() {
// Your code here
if (horizontalWin()) {
printBoard(); // refreshes the screen and shows the last play prior to the message below.
console.log(`Congratulations player ${playerTurn}. You notched a horizontal win`);
return true;
} else if (verticalWin()) {
printBoard(); // refreshes the screen and shows the last play prior to the message below.
console.log(`Congratulations player ${playerTurn}. You notched a vertical win`);
return true;
} else if (diagonalWin()) {
printBoard(); // refreshes the screen and shows the last play prior to the message below.
console.log(`Congratulations player ${playerTurn}. You notched a diagonal win`);
return true;
}
return false;
}

function ticTacToe(row, column) {
// Your code here
// Program will control player turns using X's and O's
// Based on row and column, populate array position in board
// Test for 0, 1, 2 indexes. User cannot enter anything else.
// Make sure square is available.
// Now check for a win. Can be horizontal, vertical or diagonal.
// If no one wins, switch to the next player and repeat.

const validValue = (myIndex) => {
const valuesArr = [0,1,2];
return valuesArr.some(validIndex => myIndex == validIndex);
}

if (validValue(row) && validValue(column)) { // This test makes sure values entered are 0, 1, 2 and nothing else.
if (!board[row][column].trim() ) { // This test makes sure the square is empty.
board[row][column] = playerTurn; // set the square equal to the current player.
turnCount++;
if (checkForWin()) { // checkForWin returns TRUE if someone won. It returns FALSE if no one has won yet.
console.log(`Yes we have a winner folks... player ${playerTurn}. Start a new game`);
return true; // returning true ends the game. We have a winner.
} else if (turnCount === 9) {
printBoard(); // refreshes the screen and shows the last play prior to the message below.
console.log(`Yes we have a tie folks. No one won. Start a new game`);
return true; // returning true ends the game. We have a winner.
} else {
// Ok no one has won yet.
// this logic controls who's turn it is.
playerTurn === 'X'? playerTurn = 'O' : playerTurn = 'X';
}
} else {
console.log('Hey, that square is already filled in. Select another');
}
} else {
console.log('Please enter a valid index. Valid values are 0, 1, 2');
}
return false; // returning false keeps the game going.
}

function getPrompt() {
printBoard();
console.log("It's Player " + playerTurn + "'s turn.");
rl.question('row: ', (row) => {
rl.question('column: ', (column) => {
ticTacToe(row, column);
getPrompt();
// I wrapped ticTacToe function around a condition so I can "end" the game. ticTacToe returns TRUE if someone won the game
// or there is a tie. The game is over. It returns FALSE if the game is still going on.
if (!ticTacToe(row, column)) {
getPrompt();
} else {
process.exit(0); // this command exits the Program
}
});
});

}



// Tests

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