From 9dc3d1aded9d05c7f5b865629d8de8768d92a3aa Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 27 Jul 2017 05:23:27 -0500 Subject: [PATCH 1/6] Adding partnerChallenge.js and white board of towers of Hanoi --- 03week/partnerChallenge.js | 33 +++++++++++++++++++++++++++++++++ 03week/towersOfHanoi.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 03week/partnerChallenge.js diff --git a/03week/partnerChallenge.js b/03week/partnerChallenge.js new file mode 100644 index 000000000..09a2adcaf --- /dev/null +++ b/03week/partnerChallenge.js @@ -0,0 +1,33 @@ +'use strict' + +const partnerObj = { + firstName:'Craig', + lastName:'Copeland', + address1:'904 Prize Oaks Dr', + address2:'', + city:'Cedar Park', + state:'TX', + zip:'78613', + printAddress: () => { + console.log(partnerObj.address1); + console.log(partnerObj.address2); + console.log(`${partnerObj.city} ${partnerObj.state} ${partnerObj.zip}`); + } + +}; + +partnerObj.printAddress(); +console.log(`Hello ${partnerObj.firstName} ${partnerObj.lastName}`); + +partnerObj.lastName = 'cars'; + + +const partnerArr = Object.keys(partnerObj); +console.log(partnerArr , 'partner Array'); + + +for (let i=0; i < partnerArr.length; i++) { + console.log(partnerArr[i]); + console.log(partnerObj[partnerArr[i]]); + +} diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 165912ed8..69431d32a 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -35,7 +35,42 @@ function checkForWin() { } function towersOfHanoi(startStack, endStack) { - // Your code here + // first, make sure the user typed in either stack a, b, or c. User must enter a, b, c. Nothing else. + // the source stack selected must not be empty. There should be a disk there. + // (isLegal): If the last value (selected disk) on source stack is smaller than the last value on the target stack + // then call: (movePiece). Otherwise, prompt the user to try again. Clearly, the user is making an illegal move. + // (checkForWin) If stack b or stack c has all the disks, WIN. + + // If valid entries then + // if isLegal() then + // movePiece() + // if checkForWin() then + // Announce winner! + // else + // Continue playing game + // end if + // else + // Tell user move cannot be made. Make another selection + // end if + // else + // Tell user to enter correct stack values + // end if + + // isLegal(sourceStack, targetStack) // the tests expect this function to take in 2 values. + // if source stack array.last < target stack array.last then + // return true + // end if + // return false + + // movePiece() + // target stack array.push(source stack array.pop) + // return + + // checkForWin() + // if b.array.length === 4 || c.array.length === 4 then + // return true + // end if + // return false } From 4bf525470f9fc3d740525bdea3f94ff0cea8d1ac Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 27 Jul 2017 05:57:45 -0500 Subject: [PATCH 2/6] Built skeleton code for Towers of Hanoi --- 03week/towersOfHanoi.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 69431d32a..49be7881b 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -19,12 +19,12 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { +function movePiece(sourceStack,targetStack) { // Your code here } -function isLegal() { +function isLegal(sourceStack, targetStack) { // Your code here } @@ -71,6 +71,26 @@ function towersOfHanoi(startStack, endStack) { // return true // end if // return false + const validUserEntry = (myStack) => { + const validStacksArr = ['a','b','c']; + return validStacksArr.some(validStack => myStack === validStack); + } + + startStack = startStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. + endStack = endStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. + + if (validUserEntry(startStack) && validUserEntry(endStack)) { + if (isLegal(startStack, endStack)) { + movePiece(startStack, endStack); + if (checkForWin()) { + console.log('Congratulations! You won!'); + } + } else { + console.log('That is an illegal move. Cannot place a larger disk on a smaller one. Try again'); + } + } else { + console.log('Please enter correct stack name... a, b, or c'); + } } From fc09e4d0e82576d9013619a7cf453f14b42dd45e Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Thu, 27 Jul 2017 07:26:54 -0500 Subject: [PATCH 3/6] Towers of Hanoi pretty much complete. --- 03week/towersOfHanoi.js | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 49be7881b..4e75f9067 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -13,25 +13,44 @@ let stacks = { c: [] }; +const winCount = stacks.a.length; +let message = ''; + function printStacks() { console.log("a: " + stacks.a); console.log("b: " + stacks.b); console.log("c: " + stacks.c); } -function movePiece(sourceStack,targetStack) { - // Your code here - +function movePiece(startStack,endStack) { + // const disktoMove = stacks[startStack].pop(); + // console.log(disktoMove); + stacks[endStack].push(stacks[startStack].pop()); } -function isLegal(sourceStack, targetStack) { - // Your code here +function isLegal(startStack, endStack) { + // console.log('Start: ' + stacks[startStack][stacks[startStack].length-1]); + // console.log(`End: ${stacks[endStack].length}`); + if (stacks[startStack].length === 0) { + return false; + } else if (stacks[endStack].length === 0) { + return true; + } else { + return stacks[startStack][stacks[startStack].length-1] < stacks[endStack][stacks[endStack].length-1]; + } } function checkForWin() { - // Your code here - + // console.log(`win count: ${winCount}`); + // console.log(`stack a count: ${stacks.a.length}`); + // console.log(`stack b count: ${stacks.b.length}`); + // console.log(`stack c count: ${stacks.c.length}`); + if (stacks.b.length === winCount || stacks.c.length === winCount) { + return true; + } + return false; + // stacks.b.length === winCount || stacks.c.length === winCount ? (return true) : (return false); } function towersOfHanoi(startStack, endStack) { @@ -86,7 +105,10 @@ function towersOfHanoi(startStack, endStack) { console.log('Congratulations! You won!'); } } else { - console.log('That is an illegal move. Cannot place a larger disk on a smaller one. Try again'); + console.log('That is an illegal move. One of these issues... Try again\n\n'); + console.log(' a) Cannot place a larger disk on a smaller one.'); + console.log(' b) Start stack is empty'); + } } else { console.log('Please enter correct stack name... a, b, or c'); From fce8b47f9ce1c14346c8449fed74ea6826072be8 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Fri, 28 Jul 2017 00:16:32 -0500 Subject: [PATCH 4/6] Added the ability to exit the game. Added some comments --- 03week/towersOfHanoi.js | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 4e75f9067..9dfa97fdf 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -8,7 +8,7 @@ const rl = readline.createInterface({ }); let stacks = { - a: [4, 3, 2, 1], + a: [3, 2, 1], b: [], c: [] }; @@ -22,12 +22,16 @@ function printStacks() { console.log("c: " + stacks.c); } +// At this point in the code, the user data has been vetted and the move has been confirmed to be legal. +// This function simply pops the top "disc" off of start Stack and pushes in on end Stack function movePiece(startStack,endStack) { // const disktoMove = stacks[startStack].pop(); // console.log(disktoMove); stacks[endStack].push(stacks[startStack].pop()); } +// The user data has been tested to make sure they are valid values. Now we need to test a bit further to see if the +// move from source to end is valid. This function does that. function isLegal(startStack, endStack) { // console.log('Start: ' + stacks[startStack][stacks[startStack].length-1]); // console.log(`End: ${stacks[endStack].length}`); @@ -41,6 +45,7 @@ function isLegal(startStack, endStack) { } } +// Function tests to see if all the discs are on the other stacks. If they are, that is a WIN. function checkForWin() { // console.log(`win count: ${winCount}`); // console.log(`stack a count: ${stacks.a.length}`); @@ -53,7 +58,9 @@ function checkForWin() { // stacks.b.length === winCount || stacks.c.length === winCount ? (return true) : (return false); } +// this function is the main driver function. function towersOfHanoi(startStack, endStack) { + /****************************** WHITE BOARD NOTES ******************************* // first, make sure the user typed in either stack a, b, or c. User must enter a, b, c. Nothing else. // the source stack selected must not be empty. There should be a disk there. // (isLegal): If the last value (selected disk) on source stack is smaller than the last value on the target stack @@ -90,6 +97,9 @@ function towersOfHanoi(startStack, endStack) { // return true // end if // return false + *************************************************************************/ + + // Tests to make sure user entries are only a, b, or c. const validUserEntry = (myStack) => { const validStacksArr = ['a','b','c']; return validStacksArr.some(validStack => myStack === validStack); @@ -98,14 +108,15 @@ function towersOfHanoi(startStack, endStack) { startStack = startStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. endStack = endStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. - if (validUserEntry(startStack) && validUserEntry(endStack)) { - if (isLegal(startStack, endStack)) { - movePiece(startStack, endStack); - if (checkForWin()) { + if (validUserEntry(startStack) && validUserEntry(endStack)) { // test to make sure a, b, or c is entered. + if (isLegal(startStack, endStack)) { // test to make sure move is legal + movePiece(startStack, endStack); // Confirmed it is a legal move. Go ahead and pop disc from startStack and push to endStack + if (checkForWin()) { // See if all discs have been moved to stack b or c. If so, then WIN. console.log('Congratulations! You won!'); + return true; // Returning true ends the game. We have a winner. } } else { - console.log('That is an illegal move. One of these issues... Try again\n\n'); + console.log('That is an illegal move. One of these issues... Try again\n'); console.log(' a) Cannot place a larger disk on a smaller one.'); console.log(' b) Start stack is empty'); @@ -114,14 +125,20 @@ function towersOfHanoi(startStack, endStack) { console.log('Please enter correct stack name... a, b, or c'); } + return false; // No win yet. Returning false keeps the game going. } function getPrompt() { printStacks(); rl.question('start stack: ', (startStack) => { rl.question('end stack: ', (endStack) => { - towersOfHanoi(startStack, endStack); - getPrompt(); + // I wrapped towersOfHanoi function around a condition so I can "end" the game. towersOfHanoi returns TRUE if someone + // won the game. It returns FALSE if the game is still going on. + if (!towersOfHanoi(startStack, endStack)) { + getPrompt(); + } else { + process.exit(0); // this command exits the Program + } }); }); } From 5aa759727816bbb75fd5c1792d6f8f4d57719fcb Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Fri, 28 Jul 2017 00:24:10 -0500 Subject: [PATCH 5/6] ToH Officially complete: a) Ran linter, b) cleaned up comments. --- 03week/towersOfHanoi.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 9dfa97fdf..1d13d6cf6 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -105,15 +105,15 @@ function towersOfHanoi(startStack, endStack) { return validStacksArr.some(validStack => myStack === validStack); } - startStack = startStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. - endStack = endStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. + startStack = startStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. + endStack = endStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. if (validUserEntry(startStack) && validUserEntry(endStack)) { // test to make sure a, b, or c is entered. - if (isLegal(startStack, endStack)) { // test to make sure move is legal - movePiece(startStack, endStack); // Confirmed it is a legal move. Go ahead and pop disc from startStack and push to endStack - if (checkForWin()) { // See if all discs have been moved to stack b or c. If so, then WIN. + if (isLegal(startStack, endStack)) { // test to make sure move is legal + movePiece(startStack, endStack); // Confirmed it is a legal move. Go ahead and pop disc from startStack and push to endStack + if (checkForWin()) { // See if all discs have been moved to stack b or c. If so, then WIN. console.log('Congratulations! You won!'); - return true; // Returning true ends the game. We have a winner. + return true; // Returning true ends the game. We have a winner. } } else { console.log('That is an illegal move. One of these issues... Try again\n'); @@ -125,7 +125,7 @@ function towersOfHanoi(startStack, endStack) { console.log('Please enter correct stack name... a, b, or c'); } - return false; // No win yet. Returning false keeps the game going. + return false; // No win yet. Returning false keeps the game going. } function getPrompt() { From 045ea83687a9d090f5de61d207cc37fc40b2cdef Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Fri, 28 Jul 2017 00:35:21 -0500 Subject: [PATCH 6/6] Improved checkForWin... now only a single return. --- 03week/towersOfHanoi.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 1d13d6cf6..d5aa8d7fd 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -51,11 +51,7 @@ function checkForWin() { // console.log(`stack a count: ${stacks.a.length}`); // console.log(`stack b count: ${stacks.b.length}`); // console.log(`stack c count: ${stacks.c.length}`); - if (stacks.b.length === winCount || stacks.c.length === winCount) { - return true; - } - return false; - // stacks.b.length === winCount || stacks.c.length === winCount ? (return true) : (return false); + return stacks.b.length === winCount || stacks.c.length === winCount; } // this function is the main driver function.