From c66234a89a8d06a622f0a8291c02bacd286d3b57 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Tue, 25 Jul 2017 18:40:22 -0500 Subject: [PATCH 1/7] 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 5c8278266c49192041b123f5a53227db58903bd1 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Tue, 25 Jul 2017 20:37:01 -0500 Subject: [PATCH 2/7] Partner Challenge --- 02week/ticTacToe.js | 12 +++++++++++- 03week/partnerChallenge.js | 24 ++++++++++++++++++++++++ 03week/towersOfHanoi.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 03week/partnerChallenge.js diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index c3ed51eb2..6bf0f3e74 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/column +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] @@ -32,7 +43,6 @@ function printBoard() { 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!'); }); diff --git a/03week/partnerChallenge.js b/03week/partnerChallenge.js new file mode 100644 index 000000000..efd1aadb9 --- /dev/null +++ b/03week/partnerChallenge.js @@ -0,0 +1,24 @@ +'use strict' + +const partnerObj = { + firstName: "Renata", + lastName: "Estes", + age:27, + eyeColor: "brown", + talk: () => { + return "Hello Renee!" + }, +}; + +partnerObj.lastName = "cars"; +console.log(partnerObj['firstName']); +console.log(partnerObj.lastName); +console.log(partnerObj.talk()); + +const partnerArr = Object.keys(partnerObj); +console.log(partnerArr); + +for (var i in partnerArr){ + console.log(partnerArr[i]) + console.log(partnerObj[partnerArr[i]]) +} diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 165912ed8..eeab4d269 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,5 +1,39 @@ 'use strict'; +var clicker = function() { + // Put app logic here +var $block = null; +var m = 0; + $("[data-stack]").click(function(){ + m = m +1; + $("#counter").html(m); + var $data_stack = $(this); + var $children = $data_stack.children(); + var $lastChild = $children.last(); + + if ($block == null) { + $block = $lastChild.detach(); + } else { + if ( $lastChild.data("block") < $block.data("block")) { + alert("Invalid Move"); + } else { + $(this).append($block); + $block = null; + } + } + if ( $("[data-stack='2']").children().length >= 4 || $("[data-stack='3']").children().length >= 4 ) + { + alert("You Win!!"); + } + +}); +} + +$(document).ready(clicker); + + + + const assert = require('assert'); const readline = require('readline'); const rl = readline.createInterface({ From 4b69da8d34de055f8ff999f05271b01f2233bf38 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Thu, 27 Jul 2017 18:43:34 -0500 Subject: [PATCH 3/7] Towers of Hanoi --- 02week/ticTacToe.js | 20 ++++---- 03week/partnerChallenge.js | 4 +- 03week/towersOfHanoi.js | 93 +++++++++++++++++++++++++------------- 3 files changed, 74 insertions(+), 43 deletions(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 6bf0f3e74..876a2b9d6 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -42,28 +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 diff --git a/03week/partnerChallenge.js b/03week/partnerChallenge.js index efd1aadb9..ed894d14c 100644 --- a/03week/partnerChallenge.js +++ b/03week/partnerChallenge.js @@ -19,6 +19,6 @@ const partnerArr = Object.keys(partnerObj); console.log(partnerArr); for (var i in partnerArr){ - console.log(partnerArr[i]) - console.log(partnerObj[partnerArr[i]]) + console.log(partnerArr[i]) + console.log(partnerObj[partnerArr[i]]) } diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index eeab4d269..cf76a2ffa 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,37 +1,47 @@ 'use strict'; -var clicker = function() { - // Put app logic here -var $block = null; -var m = 0; - $("[data-stack]").click(function(){ - m = m +1; - $("#counter").html(m); - var $data_stack = $(this); - var $children = $data_stack.children(); - var $lastChild = $children.last(); - - if ($block == null) { - $block = $lastChild.detach(); +//Create three CoRs with arrays +//The first CoR is full +let stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] +}; +//You click on the first CoR to remove a stack (pop) +//You click on one of the other CoR to add that stack you removed (movePiece) +//~>You must check the CoR to see if there is already a stack there. +(push)function movePiece() { + if (stacks == null) { + stacks.pop(); + } else { + if (last) < (last) { + alert("Invalid Move"); } else { - if ( $lastChild.data("block") < $block.data("block")) { - alert("Invalid Move"); - } else { - $(this).append($block); - $block = null; - } + stacks.push(); + stacks = null; } - if ( $("[data-stack='2']").children().length >= 4 || $("[data-stack='3']").children().length >= 4 ) - { - alert("You Win!!"); } + //(Compare array values). If there is, you need to check the value of that stack to make sure it is bigger than the one you want to drop. +//You win whenever you get the entire stack on one of the other CoRs (stack.b or stack.c) -}); -} - -$(document).ready(clicker); +var clicker = function() { + // + var row = null; + var m = 0; + stack.click(function(){ + m = m +1; + $("#counter").html(m); + var stack = $(this); + var children = stack.children(); + var lastChild = children.last(); + if ((stack ='2']).children().length >= 4) || ((stack ='3']).children().length >= 4) + { + alert("You Win!!"); + } + }); +} const assert = require('assert'); @@ -47,6 +57,12 @@ let stacks = { c: [] }; +let first = stacks[0]; +console.log(first); + +let last = stacks[stacks.length - 1]; +console.log(last); + function printStacks() { console.log("a: " + stacks.a); console.log("b: " + stacks.b); @@ -54,9 +70,16 @@ function printStacks() { } function movePiece() { - // Your code here - -} + if (stacks == null) { + stacks.pop(); + } else { + if (last) < (last) { + alert("Invalid Move"); + } else { + stacks.push(); + stacks = null; + } + } function isLegal() { // Your code here @@ -64,8 +87,9 @@ function isLegal() { } function checkForWin() { - // Your code here - + if (stacks.b.length === 4 || stacks.c.length === 4) { + console.log("You win"); + } } function towersOfHanoi(startStack, endStack) { @@ -125,3 +149,10 @@ if (typeof describe === 'function') { getPrompt(); } + + +//function towersOfHanoi (startStack, endStack) +//if sourceStack arry.last < targetStack array.last then +// return true +// end if +//return false From e49dd1f6da54074d38b421f81010f568291e317f Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Sun, 30 Jul 2017 20:20:32 -0500 Subject: [PATCH 4/7] partnerChallenge --- 03week/partnerChallenge.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/03week/partnerChallenge.js b/03week/partnerChallenge.js index ed894d14c..fe1e602d3 100644 --- a/03week/partnerChallenge.js +++ b/03week/partnerChallenge.js @@ -19,6 +19,6 @@ const partnerArr = Object.keys(partnerObj); console.log(partnerArr); for (var i in partnerArr){ - console.log(partnerArr[i]) - console.log(partnerObj[partnerArr[i]]) +console.log(partnerArr[i]) +console.log(partnerObj[partnerArr[i]]) } From 50232c9259e10f14cce7f6eee6747ae2a3d91b79 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Sun, 30 Jul 2017 20:30:21 -0500 Subject: [PATCH 5/7] partnerChallenge --- 03week/partnerChallenge.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/03week/partnerChallenge.js b/03week/partnerChallenge.js index fe1e602d3..ed894d14c 100644 --- a/03week/partnerChallenge.js +++ b/03week/partnerChallenge.js @@ -19,6 +19,6 @@ const partnerArr = Object.keys(partnerObj); console.log(partnerArr); for (var i in partnerArr){ -console.log(partnerArr[i]) -console.log(partnerObj[partnerArr[i]]) + console.log(partnerArr[i]) + console.log(partnerObj[partnerArr[i]]) } From 08b53e9b821d4da3e2b87c6b1150c982c1b68745 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Sun, 30 Jul 2017 21:03:49 -0500 Subject: [PATCH 6/7] 03week/towersOfHanoi.js --- 03week/forLoops.js | 38 ++++++++++++++++++++++++++++++++++++++ 03week/towersOfHanoi.js | 19 ++++++++++--------- 2 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 03week/forLoops.js diff --git a/03week/forLoops.js b/03week/forLoops.js new file mode 100644 index 000000000..8d4134d21 --- /dev/null +++ b/03week/forLoops.js @@ -0,0 +1,38 @@ +const carsInReverse = ['Fiat', 'Ford']; +console.log(carsInReverse); + +for (let i = 0; i < carsInReverse.length; i++){ + console.log(carsInReverse[i]); +} + +const persons = { +firstName: "Jane", +lastName: "Doe", +birthDate: "Jan 5, 1925", +gender: "female" +}; + +let key; +for (key in persons) { + console.log(key); +} + +for (key in persons) { + if (key ==='birthDate') { + console.log(persons['birthDate']); + } +}; + + +let n = 0; +while (n < 1001) { + console.log(n); + n++ +}; + +/* +A "for loop" is better than a "while loop" when you know the exact number of iterations you need to do for a specific problem. + +The difference between a for loop and a for...in loop is a for loop is used to iterate through an array and a for..in loop is to iterate over the properties of an object. + +The difference between a while loop and a do...while loop is a while loop keeps going until something rings true. The do...while loop when some sort of task or function needs to be done first before you start running that loop. diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index cf76a2ffa..7352985bc 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -10,11 +10,12 @@ let stacks = { //You click on the first CoR to remove a stack (pop) //You click on one of the other CoR to add that stack you removed (movePiece) //~>You must check the CoR to see if there is already a stack there. -(push)function movePiece() { +//(push)// +function movePiece() { if (stacks == null) { stacks.pop(); } else { - if (last) < (last) { + if (last < last) { alert("Invalid Move"); } else { stacks.push(); @@ -35,7 +36,7 @@ var clicker = function() { var children = stack.children(); var lastChild = children.last(); - if ((stack ='2']).children().length >= 4) || ((stack ='3']).children().length >= 4) + if ((stack ='2').children().length >= 4 || (stack ='3').children().length >= 4) { alert("You Win!!"); } @@ -73,7 +74,7 @@ function movePiece() { if (stacks == null) { stacks.pop(); } else { - if (last) < (last) { + if (last < last) { alert("Invalid Move"); } else { stacks.push(); @@ -151,8 +152,8 @@ if (typeof describe === 'function') { } -//function towersOfHanoi (startStack, endStack) -//if sourceStack arry.last < targetStack array.last then -// return true -// end if -//return false +/*function towersOfHanoi (startStack, endStack) +if sourceStack arry.last < targetStack array.last then + return true + end if +return false*/ From a42fd5dea64eb258f591ec2a150b11e8a779fdd9 Mon Sep 17 00:00:00 2001 From: Renata Estes Date: Sun, 30 Jul 2017 21:11:06 -0500 Subject: [PATCH 7/7] tictactoe --- 02week/ticTacToe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/02week/ticTacToe.js b/02week/ticTacToe.js index 876a2b9d6..e31091366 100644 --- a/02week/ticTacToe.js +++ b/02week/ticTacToe.js @@ -8,7 +8,7 @@ //const updatedRow = board.slice(row, row+1) [0]; //console.log(updatedRow, 'Row') -/*Check it marker exists at row/column +/*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]