title: JavaScript: Hex to decimal converter error
body: <p>I have been making a custom function for converting hex to decimal from my <a href="https://scratch.mit.edu/projects/102878428/#player" rel="nofollow noreferrer">scratch project</a>:</p>
  
  <pre><code>function Hex2Decimal(hex){
      var deci = 0;
      var num = 1;
      var hexstr = String(hex);
      hexstr = hexstr.toLowerCase();
      var expon = 0;
      for(var i = 0; i &lt; hex.length; i++){
          expon = Math.pow(16,hexstr.length - (num+1));
          if(hexstr[num+1] === "a"){
              deci = (10*expon)+deci;
          }else if(hexstr[num-1] === "b"){
              deci = (11*expon)+deci;
          }else if(hexstr[num-1] === "c"){
              deci = (12*expon)+deci;
          }else if(hexstr[num-1] === "d"){
              deci = (13*expon)+deci;
          }else if(hexstr[num-1] === "e"){
              deci = (14*expon)+deci;
          }else if(hexstr[num-1] === "f"){
              deci = (15*expon)+deci;
          }else if(hexstr[num-1] != "undefined"){
              deci = (Number(hexstr[num-1])*expon)+deci;
          }
          num = num + 1;
      }
      return deci;
  }
  </code></pre>
  
  <p>but when I put "BC324240" into it, it returns the value '197338148' instead of '3157410368.'
  When converting the value back to hex, I get 'BC32424.' For some reason, that I need help finding, the '0' in it is completely 'ignored.'
  Also noticed that using '10' returns 1...</p>