Skip to content
Open
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
88 changes: 70 additions & 18 deletions 03week/towersOfHanoi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
'use strict';

//whiteboarding
//getpromt to ask from which stack to move and where to.
//if endstack is empty then move, if not then check legal
//move has to be legal, (isLegal)
//if start number is smaller than end number then move piece
//if illegal move tell them and get prompt again
//if legal, pop off of initial stack and push onto next stack (movePiece)
//check for win
//if not win, reprompt
//if win, tell user you won, reset stack (stackReset)


const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
Expand All @@ -17,27 +29,65 @@ function printStacks() {
console.log("a: " + stacks.a);
console.log("b: " + stacks.b);
console.log("c: " + stacks.c);
}

function movePiece() {
// Your code here

}

function isLegal() {
// Your code here
};

}
const movePiece = (startStack, endStack) => {
const start = stacks[startStack];
const end = stacks[endStack];
end.push(start.pop());
};

function checkForWin() {
// Your code here
const isLegal = (startStack, endStack) => {
const SSLastNum = stacks[startStack][stacks[startStack].length-1];

Choose a reason for hiding this comment

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

These variable names should start with a lowercase letter. The convention is upper case would signify a class.

const ESLastnum = stacks[endStack][stacks[endStack].length-1];
if (ESLastnum > SSLastNum) {
return true;

Choose a reason for hiding this comment

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

this can be simplified, just return the evalutation

} else {
return false;
};
};

}
const checkForWin = () => {
let checkWin = false;

Choose a reason for hiding this comment

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

This function would always return a truthy value ('You win' or 'You suck') so your parent function that checks for the return would always reset the stack. What would be better is if you just returned a truthy or falsy value and have the parent function return the message.

const solution = [4, 3, 2, 1];
for (index = 0; index < 4; index++) {

Choose a reason for hiding this comment

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

this loop is unnecessary, just check if stack b or c has a length of 4

if (stacks['c'][index] == solution[index]) {
checkWin = true;
} else {
checkWin = false;
break;
};
};
if(checkWin) {
return "You win!";
} else {
return "You suck";
};
};

function towersOfHanoi(startStack, endStack) {
// Your code here
const resetStack = () => {
let stacks = {

Choose a reason for hiding this comment

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

you shouldn't redeclare here, stacks were defined at line 22

a: [4, 3, 2, 1],
b: [],
c: []
};
};

}
const towersOfHanoi = (startStack, endStack) => {
if (stacks[endStack].length == 0) {
movePiece(startStack, endStack);
} else {
if (isLegal(startStack, endStack)) {

Choose a reason for hiding this comment

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

good function organization

movePiece(startStack, endStack);
if (checkForWin()) {
alert('Congrats!');
resetStack();
}
} else {
alert('Please enter a legal move')
}
}
};

function getPrompt() {
printStacks();
Expand All @@ -47,7 +97,7 @@ function getPrompt() {
getPrompt();
});
});
}
};

// Tests

Expand Down Expand Up @@ -80,8 +130,10 @@ if (typeof describe === 'function') {
});
describe('#checkForWin()', () => {
it('should detect a win', () => {
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
stacks = { a: [], b: [], c: [4, 3, 2, 1] };
assert.equal(checkForWin(), true);
stacks = { a: [], b: [4, 3, 2, 1], c: [] };
assert.equal(checkForWin(), false);
stacks = { a: [1], b: [4, 3, 2], c: [] };
assert.equal(checkForWin(), false);
});
Expand Down