var threeSumClosest = function(nums, target) {
let ans = nums[0] + nums[1] + nums[2];
const len = nums.length;
nums.sort((a, b) => a - b); // 排序
for (let i = 0; i < len ; i++) {
let L = i+1;
let R = len-1;
while(L < R){
const sum = nums[i] + nums[L] + nums[R];
const T = Math.abs(sum - target) < Math.abs(ans - target)
if(T){
ans = sum
}
if(sum > target){
R --;
} else if(sum < target) {
L ++;
} else {
return ans
}
}
}
return ans;
};