0

Header: I am console.logging what I want to return.

I want to have a data structure store the necessary powers of 2 that I am calculating, but because it's recursive, I'm having trouble saving the output in a string or array without the data structure being overwritten each function call.

It's almost like I want to use request.session. How do I store what I'm console logging?

I tried:

if(!this) {then that}

But this is never there so it runs every time. Maybe passing it through the parameter is a way to solve this but the parameter is only n, and I want the function to only take in n and then output some information relevant to the binary equivalent, but I am probably going about it all wrong.

I think it's interesting that I can console.log it but I can't push it to an array.

How can I proceed?

function binary(n){
    if(n == 0){
        console.log("The end");
        return 0;
    }
    var b, x;
    for(var i = 0; i <= 10; i++){
        b = Math.pow(2,i);
        if(b > n){
            var p = i-1;
            console.log("2 to the",p, "is on");
            x = Math.pow(2,i-1);
            n = n-x;
            return binary(n);
        }
    }
}
binary(12);
binary(365);
// 12 == 2^3(1) + 2^2(1) + 2^1(0) + 2^0(0)
// 12 == 1100

Footer:

[Running] node "/Users/maxwelljann/Desktop/cracking_the_coding_interview/b3.js"

2 to the 3 is on
2 to the 2 is on
The end
2 to the 8 is on
2 to the 6 is on
2 to the 5 is on
2 to the 3 is on
2 to the 2 is on
2 to the 0 is on
The end
halfer
  • 19,824
  • 17
  • 99
  • 186
  • "*without the data structure being overwritten each function call.*" - what did you try where the data structure was overwritten? – Bergi Aug 22 '18 at 08:17
  • Why do you have both a loop and recursion? – Bergi Aug 22 '18 at 08:21
  • I tried `if(!output){ var output = []; }` but it literally gets triggered no matter what. Maybe I'm confusing it with Python. As for why I have a loop+recursion. I don't know how to do better than that. the for loop is designed to get me the lowest power of 2 higher than n, then I reassign n, and do it recursively. It was just my initial idea. It's definitely broken. I mean I obviously don't know what I'm doing. The for loop is set to go to 10 simply because I didn't want to do a do while loop or a while loop. I basically decided to do recursion half way through the problem. – Maxwell Jann Aug 24 '18 at 04:04
  • I have it working. thanks for everyone's help. I'd be interested in the optimal solution but I wanted to take a stab at it myself first. – Maxwell Jann Aug 24 '18 at 04:07
  • https://github.com/codejoy-wins/Google_Interview_Prep/blob/master/fibbinary.js – Maxwell Jann Aug 24 '18 at 06:28

2 Answers2

1

Just because a function has a formal parameter doesn't mean you have to pass it in the initial call. Any parameters you don't fill get set to undefined:

function showMeTheArray (num, array) {
  console.log(array);
}

showMeTheArray(42);

And with ES2015, you can set default parameters for undefined arguments:

function showMeTheArray (num, array = [1, 2, 3]) {
  console.log(array);
}

showMeTheArray(42);
showMeTheArray(42, ['I passed it this time']);

So you can pass an array in your recursive calls to binary(), but not require it from the user:

function binary (n, array = []) {
  if (n == 0) {
    return [...array, 'The end'];
  }
  var b, x;
  for (var i = 0; i <= 10; i++) {
    b = Math.pow(2, i);
    if (b > n) {
      var p = i - 1;
      const newArray = [...array, `2 to the ${p} is on`];
      x = Math.pow(2, i - 1);
      n = n - x;
      return binary(n, newArray);
    }
  }
}
console.log(binary(12));
console.log(binary(365));
AuxTaco
  • 4,883
  • 1
  • 12
  • 27
-1

I can think of a global array to store whatever you are console logging.

 var globalArray = []; //Global array

  function binary(n){
    if(n == 0){
      console.log("The end");
      return 0;
    }
    var b, x;
    for(var i = 0; i <= 10; i++){
      b = Math.pow(2,i);
      if(b > n){
        var p = i-1;
        globalArray.push("2 to the",p, "is on"); //Add elements to the global array
        x = Math.pow(2,i-1);
        n = n-x;
        return binary(n);
      }
    }
  }

Output: The entries don't get overwritten since the variable is outside the function scope.

binary(2)
VM458:5 The end
0
globalArray
(3) ["2 to the", 1, "is on"]
binary(3)
VM458:5 The end
0
globalArray
(9) ["2 to the", 1, "is on", "2 to the", 1, "is on", "2 to the", 0, "is on"]

In general it is not great practice to use global variables, but if it just for experimenting, then well and good. check out this article for more info.

SubSul
  • 2,523
  • 1
  • 17
  • 27