javascript 不包括最低和最高工资的平均工资

8dtrkrch  于 2023-06-20  发布在  Java
关注(0)|答案(6)|浏览(106)

LEET代码问题陈述,
你得到一个唯一整数数组salary,其中salary[i]是第i个员工的工资。返回不包括最低和最高工资的员工平均工资。在实际答案的10-5范围内的答案将被接受。
我希望以let sal = salary[0]开始第一个索引,我想将其他工资与salary[0]进行比较,如果它更少,我想将较低的工资推入一个新的数组。但是我不知道如何区分最低工资或完成这个问题

/**
* @param {number[]} salary
* @return {number}
*/
var average = function(salary) {
    let sal = salary[0]
    for(let i = 1; i < salary.length; i++){
        if (sal > salary[i]) {
            salary.map()
        }
    }
 

};
neekobn8

neekobn81#

为此,我将对数组进行排序,然后删除第一个和最后一个元素,因为第一个元素将是最低的工资,最后一个元素将是最高的工资。然后,使用修剪后的数组,我会计算平均值:

var average = (salary) => {
    const sortedArray = salary.sort((a, b) => a - b);

    const trimmedArray = sortedArray.slice(1, -1);

    const sum = trimmedArray.reduce((prev, curr) => prev + curr)

    return sum / trimmedArray.length;
}

编辑

根据@ManuelMB的评论,我调整了sort方法,通过回调函数强制按数值排序,而不是数组中项的字符串值。

aydmsdu9

aydmsdu92#

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
var average = function(salary) { 
    const max = Math.max(...salary)
    const min = Math.min(...salary)
        
    const maxIndex = salary.indexOf(max)
    const minIndex = salary.indexOf(min)
        
    salary.splice(maxIndex, 1); // Remove one item at index maxIndex
    salary.splice(minIndex, 1); // Remove one item at index minIndex
        
    const sum = salary.reduce((pre,curr)=>pre+curr,0) 
    const elements = salary.length
    return sum/elements
};
console.log(average(array)) // 5.5
eoxn13cs

eoxn13cs3#

下面的代码通过一次查看每个工资并评估其对计数、总数、最小值和最大值的影响来计算平均值。
在计算工资时,还不知道哪些是最低工资,哪些是最高工资,因此先将 * 所有 * 工资相加,最后再减去最低工资和最高工资。

var salaries = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var count = 0;
var total = 0;
var min = Infinity;
var max = -Infinity;
for (var salary of salaries) {
  count++;
  total += salary;
  if (salary < min) min = salary;
  if (salary > max) max = salary;
}
console.log((total - min - max) / (count - 2));
wvyml7n5

wvyml7n54#

const average = (salaries) => {
    if(salaries.length <= 2) return 0;
    // This could be done in one pass, but we do it like this for the sake of convenience
    // The max/min could technically be found in one iteration but the JS Math library does not support it,
    // it would be rather trivial to implement one if this became a performance issue, but it likely wont
    const max = Math.max(...salaries);
    const min = Math.min(...salaries);

    // take advantage of the fact that all salaries are unique, so we can do direct comparisons.
    const net = salaries.reduce((total, s) => s != min && s != max ? total + s : total , 0);
    return net / (salaries.length - 2);
}
uujelgoq

uujelgoq5#

首先,我们对salary数组进行排序,然后通过slice(1,-1)删除最低工资和最高工资。
对于sort函数,我们需要一个比较函数,因为默认的sort函数像字符串一样对数字进行排序:1, 10, 2, 3, ...
然后用削减后的工资我们可以做平均值。
我还认为,如果salary.length小于3,则解决方案应该计算情况。

var salary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function average(salary)
{
  var sorted = salary.sort(compareNumbers),
      trimmedSalary = sorted.slice(1,-1),
      sum = 0,
      len = trimmedSalary.length,
      i = len;
  
  if(salary.length == 2)
    return(salary[0] + salary[1]) / 2;
  
  if(!len) //if salary.length == 1
    return sorted[0];
  
  while(i--)
    sum += trimmedSalary[i];

  return sum / len
}

function compareNumbers(a, b)
{
  return a - b
}

console.log(average(salary)); //5.5
console.log(average([4,10])); //7
console.log(average([10])); //10

ES6解决方案

var salary = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var averageES6 = (salary) => {
  
  if(salary.length == 2)
    return(salary[0] + salary[1]) / 2;
  
  if(salary.length == 1)
    return salary[0];
  
  var trimmedSalary = salary.sort((a, b) => a - b).slice(1, -1);

  var sum = trimmedSalary.reduce((prev, curr) => prev + curr);

  return sum / trimmedSalary.length
}

console.log(averageES6(salary)); //5.5
console.log(averageES6([4,10])); //7
console.log(averageES6([10])); //10
webghufk

webghufk6#

你可以先对数组进行排序,然后再做任何你想做的事情

var average = function (salary) {
    let sortedArr = salary.sort()
    let avg = 0;
    //start from 1 to ignore the first element(smallest salary) and end in salary.length-1 to ignore the last element(biggest salary)
    for (let i = 1; i < salary.length - 1; i++) {
        avg += sortedArr[i]
    }
    return avg / (salary.length - 2);

    // you can get the smallest salary by sortedArr[0]
    // you can get the biggest salary by sortedArr[sortedArr.length - 1]
};

相关问题