Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the move from source to destination.
DSA Javascript
console.log('Move disk from ' + [1] + ' to ' + dest);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dest' instead of 'source' for the starting peg.
Using an undefined variable like 'disk'.
✗ Incorrect
The move is from the source peg to the destination peg, so we print the source variable.
2fill in blank
mediumComplete the recursive call to move n-1 disks from source to temp peg.
DSA Javascript
towerOfHanoi([1] - 1, source, temp, dest);
Attempts:
3 left
💡 Hint
Common Mistakes
Passing n instead of n - 1.
Passing 1 or 0 incorrectly.
✗ Incorrect
We call the function with n-1 disks, so the argument is n - 1.
3fill in blank
hardFix the error in the base case condition to stop recursion when no disks are left.
DSA Javascript
if ([1] === 0) return;
Attempts:
3 left
💡 Hint
Common Mistakes
Checking source or dest instead of n.
Using wrong comparison operators.
✗ Incorrect
The base case checks if n is zero, meaning no disks to move.
4fill in blank
hardFill both blanks to correctly move the largest disk and then move n-1 disks from temp to dest.
DSA Javascript
console.log('Move disk from ' + [1] + ' to ' + [2]); towerOfHanoi(n - 1, temp, dest, source);
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping source and temp.
Using n instead of peg names.
✗ Incorrect
The largest disk moves from source to dest, so blanks are source and dest.
5fill in blank
hardFill all three blanks to complete the recursive calls and print statement for Tower of Hanoi.
DSA Javascript
towerOfHanoi([1] - 1, [2], [3], dest); console.log('Move disk from ' + source + ' to ' + dest); towerOfHanoi([1] - 1, temp, dest, [2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up peg names in recursive calls.
Using dest instead of temp in the first call.
✗ Incorrect
First call moves n-1 disks from source to temp, then print move from source to dest, then move n-1 disks from temp to dest using source as temp.
