title: Change Cash Function Optimization - Javascript
body: <p>Today I come to your aid in favor of optimizing some code!</p>
  
  <p>I was asked to solve a problem on a test a few weeks ago, and came up with this non-optimal solution since I'm beggining to learn Javascript.</p>
  
  <p>I wonder if there's a way (of course there is) to make this better.</p>
  
  <p>Here's the situation:</p>
  
  <p>"I am a particular kind person who has an infinite ammount of $2, $5 and $10 dollar bills, and I'm willing to change cash for anyone who wants it, BUT in the most efficient way, with the minimum ammount of bills."</p>
  
  <p>Since I had to return an object with the number of bills (or null in case of less than $2), I gave the following solution:</p>
  
  <pre><code>function giveChange(change){    
  var two = 0;
  var five = 0;
  var ten = 0;
  var changeToGo = change;
  
  while(changeToGo &gt;= 11){
      changeToGo -=10;
      ten+=1;
  }
  
  while(changeToGo &gt;=7 &amp;&amp; changeToGo &lt;=11){
      changeToGo-=5;
      five+=1;
  }
  
  while(changeToGo &gt;=2 &amp;&amp; changeToGo &lt;=6 ){
      if(changeToGo == 5){
          changeToGo -=5;
          five+=1;
  }
      else{
          changeToGo -= 2;
          two+=1;
      }
  }
  
  if (change &lt; 2){
      return null;
  }
  
  return {
      two: two,
      five: five,
      ten: ten
  };
  }
  </code></pre>
  
  <p>I think there must be a clever and more elegant solution (probably with mod% or something), but that monstrosity was what I was able to pull off at the time hahaha.</p>
  
  <p>Any thoughts?</p>