Balance a Scale with an Array of Weights
<p>I have been given the following prompt:
Suppose you have a weight on one side of a scale. Given an array of other weights, see if the scale will balance. You can use weights on either side, and you don't have to use all the weights.</p>

<p>I am trying to implement the solution using javascript and have been able to solve the problem for one of the conditions by accumulating the weights until there is match. Ultimately this will only work for the case of adding to the left side of the scale but this is not optimal considering that the weights can be added to the right side as well. Below is my implementation so far.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>const balance = (arrayOfWeights) =&gt; {
  //Sort the array and pop off the max number to be stored on the right side of the scale 
  let right = arrayOfWeights.sort().pop();
  let balanced;
  let weight;

  const subroutine = (lSide, rSide, weightList) =&gt; {
    //Determine if there is a direct match
    if(lSide === rSide) {
      balanced = true;
      return balanced;
    }
    //Return false if a match hasn't been found
    if(weightList.length === 0) {
      balanced = false;
      return balanced
    }
    //Shift the first element of the array to be added to left side
    weight = weightList.shift();
    subroutine(lSide + weight , rSide, weightList);
  }
  subroutine(0, right, arrayOfWeights);
  return balanced;
};

//Array of weights to be passed to balance function
let weights = [3,6,2,5,1];</code></pre>
</div>
</div>
</p>