arrays - Problems with Javascript Recursion -


I am currently trying to write a script that you want to get back to set the minimum number of "jumps" For an initial array of initial numbers, the initial number is always the largest number in the array.

For example, if the array [2, 3, 5, 6, 1] then the starting number will be 6 because it is the largest then you can either go to 6 positions either left or You can bounce right (you can choose) and where you end it is your next number we say that we went right, we will end up on 1 because we loop back in the beginning of the array will do. You can then go to 1 left and then return to 6 . Therefore, the minimum number of jump is 2 .

I have written the functions below to take these input array and to determine the minimum number, however,

every search function calls 2 times (Once left, and once for the right), still continuously increasing count the variable gets "confused" once the line of repetition ends (when Count> arr.length ) I hope to return it to the previous step and come back Extend the line of Rti. But the count variable does not return to the previous step (it remains the same) and therefore the entire function just stops.

TL DR: How do you keep javascript functions variable, what are they, whenever the function is called again and the variable is overwritten?

  function arising {// the greatest number of values ​​starting in POS = arr.indexOf (Math.max .apply (empty, arr)); // We are searching for multiple multi find = arr.length; Return Discovery (Early POS, Multiple, ARM, Beginner POS +00); } Function search (pos, mult, arr, target, count) {if (count> arr.length) {return false; } Var tpos = pos + 1; If ((tpos == target & count! = 0) || (tpos% target == 0 & amp; amp; amp; = 0)) {return calculation; } (Different directions = -1; direction & lt; = 1; direction + = 2) {nPos = getRealPos (pos + (direction * AR [pos]), arr); Var results = search (nPos, multi, arr, target, count + 1); If (results! = False) {return result; }}} Function getRealPos (n, numArr) {if (n> = 0) {while (n> numArr.length) {n- = numArr.length; }} And {while (n & lt; 0) {n + = numArr.length; }} Return n; }  


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -