OctaviaLo
OctaviaLo

Reputation: 1396

Replace global variable with static variable in recursive function

I have a function that logs the number of function calls it takes for the function to find a target number in an array of primes. I currently use a counter that is declared in the global scope. How do I put this counter into the function scope as a static variable so it doesn't change when the function is called each time?

let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
let counter = 1

function findPrime(arr, target){

  let guess = arr[Math.floor(arr.length/2)]

  if (guess > target){

    arr = arr.splice(0, arr.length/2)
    counter ++
    findPrime(arr,target)

  }else if (guess < target){

    arr = arr.slice(arr.length/2)
    counter ++ || 1
    findPrime(arr,target)

  }else{

    console.log('guesses taken: ' + counter)
    console.log('target is: ' + guess)

  }
}


findPrime(primes, 2)

Upvotes: 0

Views: 249

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074666

You make the function return the updated counter, so you don't have to maintain it globally; see *** comments:

let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

function findPrime(arr, target, counter) {             // ***
  if (typeof counter === "undefined") {                // ***
    counter = 1;                                       // ***
  }                                                    // ***

  let guess = arr[Math.floor(arr.length / 2)]

  if (guess > target) {

    arr = arr.splice(0, arr.length / 2)
    return findPrime(arr, target, counter + 1)         // ***

  } else if (guess < target) {

    arr = arr.slice(arr.length / 2)
    return findPrime(arr, target, counter + 1)         // ***

  } else {

    console.log('guesses taken: ' + counter)
    console.log('target is: ' + guess)
    return counter;
  }
}


findPrime(primes, 2)

In general, recursive functions accept and return information that allows them to track and control the recursion. While you can do it by closing over a non-global variable, that's not how they're usually done (though there are exceptions).

Upvotes: 2

user694368
user694368

Reputation:

You can wrap the counter in scope of function.

let primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

function findPrime(arr, target) {
    var counter = 1
    function findPrimeInner(arr, target){
        var guess = arr[Math.floor(arr.length/2)]

        if (guess > target) {
            arr = arr.splice(0, arr.length/2)
            counter ++
            findPrimeInner(arr,target)
        }else if (guess < target){

            arr = arr.slice(arr.length/2)
            counter ++ || 1
            findPrimeInner(arr,target)

        }else {
            console.log('guesses taken: ' + counter)
            console.log('target is: ' + guess)
        }
    }
    findPrimeInner(arr, target);
}

findPrime(primes, 5)

Upvotes: 0

Related Questions