title: Javascript multiplying strings
body: <p>I was doing following leetcode <a href="https://leetcode.com/problems/multiply-strings/" rel="nofollow noreferrer">question</a> on multiplying</p>
  <pre><code>Given two non-negative integers num1 and num2 represented as strings, return the
  product of num1 and num2, also represented as a string.
  
  Note: You must not use any built-in BigInteger library or convert the inputs to
  integer directly.
  
  Example 1:
  
  Input: num1 = &quot;2&quot;, num2 = &quot;3&quot; 
  Output: &quot;6&quot;
  
  Example 2:
  
  Input: num1 = &quot;123&quot;, num2 = &quot;456&quot; 
  Output: &quot;56088&quot;
  
  Constraints:
  
  - 1 &lt;= num1.length, num2.length &lt;= 200
  - num1 and num2 consist of digits only.
  - Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  </code></pre>
  <p>For this used the following approach</p>
  <ol>
  <li>Convert the string to int</li>
  <li>Multiply the int</li>
  </ol>
  <p>Algo for the same is</p>
  <pre><code>const numberMap = {
      &quot;0&quot;: 0,
      &quot;1&quot;: 1, 
      &quot;2&quot;: 2,
      &quot;3&quot;: 3, 
      &quot;4&quot;: 4,
      &quot;5&quot;: 5,
      &quot;6&quot;: 6,
      &quot;7&quot;: 7, 
      &quot;8&quot;: 8, 
      &quot;9&quot;: 9
  }
  var multiply = function(num1, num2) {
      let i = num1.length
      let j = num2.length 
      let sum = currentPower = 0
      let firstNumber = secondNumber = 0
      while(i &gt; 0 || j &gt; 0) {
          // if I or J is equal to zero, means we have itterated hence we will set the value to one 
          const firstNum = i &gt; 0 ? (numberMap[num1[i-1]]) * (10**currentPower) : 0
          const secondNum = j &gt; 0 ? (numberMap[num2[j-1]]) * (10**currentPower) : 0 
          firstNumber += firstNum
          secondNumber += secondNum
          currentPower++
          i--
          j--
      }
  
      sum = firstNumber * secondNumber
      return sum.toString()
   }; 
  </code></pre>
  <p>but when the following input is given</p>
  <pre><code>&quot;123456789&quot;
  &quot;987654321&quot;
  </code></pre>
  <p>it yields the following output <code>&quot;121932631112635260&quot;</code> instead of <code>&quot;121932631112635269&quot;</code></p>
  <p>Any idea how I can fix this?</p>