如何在javascript中获取数组的最小值和最大值

时间:2014-03-28 09:34:38

标签: javascript arrays input

嘿,我无法弄清楚如何将学生添加到数组中并获得最小值和最大值我知道我必须使用数学课但我不知道如何在javaScript中有任何想法帮助会很棒?

    var highestMark=0;
    var gradeAwarded;
    var StudentArr= [Student];
    var markArr = [mark];
    var Student = prompt("Enter Student Name: ", "Ross");
    var mark = prompt("Enter Student Mark: ", 50);
    var max = Math.max.apply(null, mark); /* This about equal to Math.max(numbers[0], ...) or Math.max(5, 6, ..) */
    var min = Math.min.apply(null, mark);
    if (mark <0 || mark >100){
alert("Grate out of bounds");
}else if (mark >= 83){
gradeAwarded = "A";
}else if (mark >= 70){
gradeAwarded = "B";
}else if (mark >= 50){
gradeAwarded = "C";
}else if (mark >= 0){
gradeAwarded = "F";
}

//document.write(Student + "  "+ gradeAwarded);
//document.write( '<br />' );

//document.write( Math.max(mark));
//document.write( Math.min(mark));

//for(var i=0;i<StudentArr.length;i++)
//document.write(Student[i]+mark[i]);
document.write(lowestMark);
//+'</td><td allign =*right*>'
//need to use push

2 个答案:

答案 0 :(得分:0)

var min = data.reduce(function(prev, cur) { return prev < cur ? prev : cur },data[0]);

答案 1 :(得分:0)

var arr = [1, 2, 3, 4, 5]
var b = arr.reduce(function(prev,next){ 
                  return prev > next ? prev:next
               });
b => 5
arr => [1, 2, 3, 4, 5]