总和没有最高和更低的数字js

时间:2016-10-17 08:58:37

标签: javascript arrays

我试图在代码大战中完成kata,但我遇到了一些问题。除了最高值之外,我想对数组中的所有元素求和。

这就是我现在所拥有的

function sumArray(array) {

  let largestVal =  Math.max(array);
  let lowestVal =  Math.min(array);
  let total =  0;

  let arrLength = array.length;

  console.log(`The lowest number is: ${lowestVal}`);
  console.log(`Array is: ${array}`);
  console.log(`Total is: ${total}`);
  console.log(`The larget number is: ${largestVal}`);
  return  // [QUESTION] <= having problems in how to return the sum of each element except the largest and lowest values.  
}

提前致谢

EDIT-1:使用@WLatif实现更新了代码

function validate(array) {
  if (typeof array === 'object' && array instanceof Array && array.length > 1) {
    return 1;
  } else {
    return 0;
  }
}

function sumArray(array) {
  if (validate(array)) {
    let sorted = array.sort(function (a, b) { return a - b });
    let largestVal = sorted.slice(-1).pop();
    let lowestVal =  sorted[0];
    let total =  array.reduce( function(prev, curr) { return prev + curr; }, 0 );
    let arrLength = array.length;

    console.log(`The lowest number is: ${lowestVal}`);
    console.log(`Array is: ${array}`);
    console.log(`Total is: ${total}`);
    console.log(`The larget number is: ${largestVal}`);
    return (total- lowestVal - largestVal);
  } else {
    console.log(`Not a valid array`);
  }

}

这是我得到的错误:

  

不是有效数组✘预期:0,而不是:undefined

我错过了什么?

以下是说明:

Sum all the numbers of the array except the highest and the lowest element (the value, not the index!).
(Only one element at each edge, even if there are more than one with the same value!)

Example:

{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6


If array is empty, null or None, or if only 1 Element exists, return 0.

3 个答案:

答案 0 :(得分:1)

这可以通过首先对数组进行排序(升序)然后执行忽略第一个(最小)和最后一个(最大)元素的操作来完成。
附件是片段 祝你好运。
修改1 :添加了验证案例。

function validate(array) {
  if (typeof array === 'object' && array instanceof Array && array.length > 1) {
    return 1;
  } else {
    return 0;
  }
}

function sumArray(array) {
  var sorted = array.sort(function(a, b) {
    return a - b
  });
  console.log(`The lowest number is: ${sorted[0]}`);
  console.log(`Array is: ${array}`);
  console.log(`Total is: ${sorted.reduce(function(a, b) { return a + b; }, 0)}`);
  console.log(`The larget number is: ${sorted.slice(-1).pop()}`);
  return sorted.slice(1, -1).reduce(function(a, b) {
    return a + b;
  }, 0);
}

// ideal case
var points = [40, 100, 1, 5, 25, 10];
if (validate(points)) {
  console.log(`sum of numbers except smallest and largest number is: ${sumArray(points)}`);
} else {
  console.log("Array validation unsuccessful");
}

//empty array case
var points = [];
if (validate(points)) {
  console.log(`sum of numbers except smallest and largest number is: ${sumArray(points)}`);
} else {
  console.log("Array validation unsuccessful");
}

// single element case
var points = [40];
if (validate(points)) {
  console.log(`sum of numbers except smallest and largest number is: ${sumArray(points)}`);
} else {
  console.log("Array validation unsuccessful");
}

//other data type case
var points = "x";
if (validate(points)) {
  console.log(`sum of numbers except smallest and largest number is: ${sumArray(points)}`);
} else {
  console.log("Array validation unsuccessful");
}

//null case
var points = null;
if (validate(points)) {
  console.log(`sum of numbers except smallest and largest number is: ${sumArray(points)}`);
} else {
  console.log("Array validation unsuccessful");
}

答案 1 :(得分:0)

function sumArray(array) {
    if (array == null)
    {
        return 0;
    }
    else if (array.length < 2)
    {
        return 0;
    }
    else
    {
        array = array.sort(function(a,b) {return a - b;});
        var total = 0;
        for (var i = 1; i < array.length - 1; i++) {
            total += array[i];
        }
        return total;
    }
}

答案 2 :(得分:-1)

我相信你想要这样的东西:

function sumArray(array) {
    let largestVal =  Math.max.apply(Math, array);
    let lowestVal =  Math.min.apply(Math, array);
    let total = array.reduce(function(v1, v2){return v1+v2}, 0);

    let arrLength = array.length;

    console.log(`The lowest number is: ${lowestVal}`);
    console.log(`Array is: ${array}`);
    console.log(`Total is: ${total}`);
    console.log(`The larget number is: ${largestVal}`);

    return total - largestVal - lowestVal;
}