881. Boats to Save People

AC code

/**
 * @param {number[]} people
 * @param {number} limit
 * @return {number}
 */
var numRescueBoats = function(people, limit) {
  const arr = people.sort((a, b) => b - a);
  let j = arr.length - 1,
      i = 0,
    n = 0;
  while (i <= j) {
    if (arr[i] + arr[j] <= limit) {
      j--;
    }
     i++;
     n++;
  }
  return n;
};