function solve(arr,sum){ let i=0 let j=arr.length-1 let first,last while(i<j){ first=arr[i] last=arr[j] const tog=first+last console.log(tog) if(tog===sum){ return [first,last] } else if(tog>sum) {j--} else {i++} } return null } console.log(solve([1,2,3,4,5],4))