twoSum

    0

    1

    YusifHasanov

    The const twoSum variable is initialized with an empty array and then each of the nums variable's length is used to initialize an individual array within the twoSum array. A for loop iterates through each of the nums variable's length and checks to see if the nums[i] + nums[j] value equals the target value. If it does, the value at position [i,j] within the result array is set to the value at position [i,j] within the nums array. Finally, the for loop breaks out of the loop.

    const twoSum = (nums, target) => {
      let result = [];
      for (let i = 0; i < nums.length; i++) {
        for (let j = i + 1; j < nums.length; j++) {
          if (nums[i] + nums[j] === target) {
            result=[i,j]
            break;
          }
        }
      }
      return result
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.