function threeSumClosest(arr, target){
let res = Number.MAX_SAFE_INTEGER;
const { length } = arr;
arr.sort((a, b) => a -b);
for(let i = 0; i < length; i++){
let left = i + 1, right = length -1;
while(left < right){
let sum = arr[left] + arr[i] + arr[right];
if(Math.abs(sum - target) < Math.abs(res - target)){
res = sum;
}
if(sum < target){
left++
}else if(sum > target){
right--
}else {
return sum;
}
}
}
return res;
}
const nums = [-1, 3, 1, -4]; target = 1;
console.log(threeSumClosest(nums, target))