Could someone let me know where am I erring?

Tell us what’s happening:

Your code so far


let arr=[];
function towerOfHanoi(n, a, b, c) {
 if(n==1)
  { 
   
    arr.push([a,b]);
   //console.log(arr);
    return arr;
  }

towerOfHanoi(n-1,a,c,b);
 arr.push([a,b]);
towerOfHanoi(n-1,c,b,a);

return arr;
}
let x=towerOfHanoi(7,'A','B','C');
console.log(x);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Towers of Hanoi

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

It worked…Thank you:)