title: Javascript twoSum algorithm: Given an array of integers, return indices of the two numbers such that they add up to a specific target
body: <blockquote>
  <p>Given an array of integers, return indices of the two numbers such that they add up to a specific target.</p>
  <p><strong>Example:</strong></p>
  <p>Given nums = <code>[3, 2, 4]</code>, target = 6,</p>
  <p>Because <code>nums[1] + nums[2]</code> = 2 + 4 = 6</p>
  <p><code>return [1, 2]</code>.</p>
  </blockquote>
  <p><em><strong>Solution</strong></em></p>
  <pre><code>var twoSum = function(nums, target) {
      for(let i = 0; i &lt;= nums.length; i++){
          for(let j = 0; j &lt;= nums.length; j++){
              if(nums[i] + nums[j] == target){
                  return [i, j]
              }
          }
      }
  };
  </code></pre>
  <p>The code above works in other cases but not this one.</p>
  <p><strong>Expected result</strong> <code>[1,2]</code></p>
  <p><strong>Output</strong> <code>[0,0]</code></p>
  <p>For instance, I've tried to use a different array of numbers and a different target and it works even if you change the order of the numbers</p>
  <p><strong>Example:</strong></p>
  <p>New array: <code>[15, 7, 11, 2]</code>, target = 9,</p>
  <p><strong>Output:</strong> <code>[1, 3]</code>.</p>
  <p>I don't understand what is wrong with the solution and I hope that someone can explain. Thanks</p>