Javascript 从特定位置之间的数组中获取最大值

Javascript 从特定位置之间的数组中获取最大值,javascript,arrays,Javascript,Arrays,我试图从数组返回最大值,但我只需要数组中某些位置的最大值: 前 var array=[1,2,3,4,5],我需要将数组[0]到数组[2]的最大值之和相加为数组[3]到数组[4](因此totalScore=max(数组[0]…数组[2])+max(数组[3]…数组[4])) 有没有办法用Math.max.apply做到这一点? 我的代码在下面+笔在这里: $(“输入[type='radio'])。单击(函数(){ var question=document.getElementsByClassN

我试图从数组返回最大值,但我只需要数组中某些位置的最大值: 前

var array=[1,2,3,4,5],我需要将数组[0]到数组[2]的最大值之和相加为数组[3]到数组[4](因此totalScore=max(数组[0]…数组[2])+max(数组[3]…数组[4]))

有没有办法用Math.max.apply做到这一点? 我的代码在下面+笔在这里:

$(“输入[type='radio'])。单击(函数(){
var question=document.getElementsByClassName(“切换”);
var totalScore=0;
变量值=[0];
对于(i=0;in!==未定义);
$(“#分数”).html(总分数);
}
}
});
函数highestVal(valueArray){
valueArray=valueArray.filter(n=>n!==未定义);
log(“这是值数组”+valueArray);
var highestVal=Math.max.apply(null,valueArray);
log(“这是最高值”+highestVal)
返回最高值;
}
函数CalcCore(scoreArray){
var sum=scoreArray.reduce((a,b)=>a+b,0)
console.log(“总和为”+Sum);
回报金额;
}
单选按钮结构(值进入questionValue数组)


0我睡觉的时间从不超过30分钟。
我至少需要30分钟才能入睡,不到一半的时间。
我至少需要30分钟才能入睡,超过一半的时间。
我需要60多分钟才能入睡,超过一半的时间。

谢谢!

您可以对数组进行切片,从中获取总和和最大值

var add=(a,b)=>a+b,
数组=[1,2,3,4,5],
max=Math.max(array.slice(0,3).reduce(add),array.slice(3,5).reduce(add));
console.log(最大值);
const sumMax=(arr,…pos)=>pos.map(p=>Math.max(…arr.slice(p[0],++p[1])).reduce((p,c)=>p+c)
常量myArray=[1,2,3,4,5]

控制台.log(SUMMAX(MyRoad,[ 0, 2),[ 3, 4 ] ])<代码> @ STE:只考虑这不是在一个提供的范围(代码> PoS<代码>代码)的意义上不在
arr
范围内或
Math。max
返回
NaN
。您可以检查输入以防止出现这种情况。是的,如果它呈现为-Infinity,则将其默认为0。知道如何在定义数组中的所有值之前计算sumMax吗?我得到了
sum=sumMax(scoreArray,[0,3],[4,4],[5,6],[7,8],[9,9],[10,10],[11,11],[12,12],[13,13]);
我想在填充值时显示更新后的总和。我试着绕了一会儿
  $("input[type='radio']").click(function() {
    var question = document.getElementsByClassName("toggle");
    var totalScore = 0;
    var questionValue = [0];  

    for (i = 0; i < question.length; i++) {
      if (question[i].type == "radio") {
        if (question[i].checked) { 
          questionValue[i] = parseInt(question[i].value, 10);
          }
        highestVal(questionValue);
        totalScore = calcScore(questionValue);
        questionValue = questionValue.filter(n=>n!==undefined);
        $("#score").html(totalScore);
      }
    }
  });

 function highestVal (valueArray){
  valueArray = valueArray.filter(n=>n!==undefined);
  console.log("This is the value array " + valueArray);
  var highestVal = Math.max.apply(null, valueArray);
  console.log("This is the highest value " + highestVal)
  return highestVal;
 }

 function calcScore (scoreArray){

   var sum = scoreArray.reduce((a, b) => a + b, 0)
   console.log("The Sum is "+ sum);
   return sum;
 }
<div class="input-control">
          <input id="1" class="toggle" name="1" value=0 type="radio">
          <label for="1" class="btn"><span>0</span> I never take longer than 30 minutes to fall asleep.</label>
          <input id="2" class="toggle " name="1" value=1 type="radio">
          <label for="2" class="btn"><span>1</span> I take at least 30 minutes to fall asleep, less than half the time.</label>
          <input id="3" class="toggle" name="1" value=2 type="radio">
          <label for="3" class="btn"><span>2</span> I take at least 30 minutes to fall asleep, more than half the time.</label>
          <input id="4" class="toggle" name="1" value=3 type="radio">
          <label for="4" class="btn"><span>3</span> I take more than 60 minutes to fall asleep, more than half the time.</label>
        </div>