Two Sum Sorted

JavaScript

const twoSumSorted = (nums, target) => {
  let left = 0;
  let right = nums.length - 1;
  let sum = 0;
  while (left < right) {
    sum = nums[left] + nums[right];
    if (sum === target) {
      return [nums[left], nums[right]];
    } else if (sum > target) {
      right--;
    } else {
      left++;
    }
  }
};

console.log(twoSumSorted([1, 4, 7, 8, 9 ,10], 12));