0

I need help on step three of solve Hanoi recursively and it says that all of my assertions do not pass and I have looked at other peoples answer and it is the same as mine. My code is Program.assertEqual(hanoi.isSolved("B"),true); that is what they say is wrong

louisfischer
  • 1,968
  • 2
  • 20
  • 38

1 Answers1

1

Step 1:

var solveHanoi = function(numDisks, fromPeg, toPeg) {
if (numDisks===0){
return;
}
// recursive case:

};

You are meant to a case of 0 discs by writing the if statement in the function.

Step 2:

var solveHanoi = function(numDisks, fromPeg, toPeg) {
if (numDisks===0){
return;
}
// recursive case:
var sparePeg = hanoi.getSparePeg(fromPeg, toPeg);
solveHanoi(numDisks-1, fromPeg, sparePeg);
};

Now create a variable called 'sparePeg' then write in the function 'solveHanoi' 3 parameters like the code above.

Step 3:

var solveHanoi = function(numDisks, fromPeg, toPeg) {
if (numDisks===0){
return;
}
// recursive case:
var sparePeg = hanoi.getSparePeg(fromPeg, toPeg);
hanoi.moveDisk(fromPeg, toPeg);
solveHanoi(numDisks-1, fromPeg, sparePeg);
};

Now add the 'hanoi.moveDisk()' function with the parameters fromPeg and sparePeg.

Step 4 & 5:

var solveHanoi = function(numDisks, fromPeg, toPeg) {
if (numDisks===0){
return;
}
// recursive case:

else{
var sparePeg = hanoi.getSparePeg(fromPeg, toPeg);
solveHanoi(numDisks-1, fromPeg, sparePeg);
hanoi.moveDisk(fromPeg, toPeg);
solveHanoi(numDisks-1, sparePeg, toPeg);
} 
};
solveHanoi(5, "A", "B");
Program.assertEqual(hanoi.isSolved("B"),true);

This is the easiest one. All you have to do is uncomment the last 2 lines of code.

Then you're finished!

Rinzler786
  • 109
  • 1
  • 11