Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript显示无限。为什么?_Javascript_Arrays - Fatal编程技术网

JavaScript显示无限。为什么?

JavaScript显示无限。为什么?,javascript,arrays,Javascript,Arrays,我只想显示最小数,但它显示最小数和无穷大,为什么我得到无穷大?我怎样才能摆脱它 var repeat, studentArr = [], markArr = []; while (repeat !== 'n' && repeat !== 'N'){ studentArr.push(prompt("Enter Student Name: ")); markArr.push(parseInt (prompt("Enter Student mark: ")));

我只想显示最小数,但它显示最小数和无穷大,为什么我得到无穷大?我怎样才能摆脱它

var repeat, studentArr = [], markArr = [];
while (repeat !== 'n' && repeat !== 'N'){
    studentArr.push(prompt("Enter Student Name: "));
    markArr.push(parseInt (prompt("Enter Student mark: ")));
    repeat = prompt ("Do you want to enter another student: y/n");
}  
var largest = max(markArr);

Array.prototype.max = function() {
  return Math.max.apply(Math, markArr);
};

Array.prototype.min = function() {
  return Math.min.apply(Math, markArr);
};
var min = Math.min.apply(Math, markArr),
    max = Math.max.apply(Math, markArr);


document.write(min);
这些不是好的原型定义;它们被拴在一个物体上!让他们改用
这个

Array.prototype.max = function() {
    return Math.max.apply(Math, this);
};

Array.prototype.min = function() {
    return Math.min.apply(Math, this);
};
除此之外,
max
在您调用它以获取
最新的
(它应该是
未定义的
)时不是一个函数,您也没有使用这些函数。我很惊讶你能得到无穷大

var min = markArr.min();
var max = markArr.max();

document.write(min);
或者我们可以停止使用原型

这些不是好的原型定义;它们被拴在一个物体上!让他们改用
这个

Array.prototype.max = function() {
    return Math.max.apply(Math, this);
};

Array.prototype.min = function() {
    return Math.min.apply(Math, this);
};
除此之外,
max
在您调用它以获取
最新的
(它应该是
未定义的
)时不是一个函数,您也没有使用这些函数。我很惊讶你能得到无穷大

var min = markArr.min();
var max = markArr.max();

document.write(min);
或者我们可以停止使用原型

这些不是好的原型定义;它们被拴在一个物体上!让他们改用
这个

Array.prototype.max = function() {
    return Math.max.apply(Math, this);
};

Array.prototype.min = function() {
    return Math.min.apply(Math, this);
};
除此之外,
max
在您调用它以获取
最新的
(它应该是
未定义的
)时不是一个函数,您也没有使用这些函数。我很惊讶你能得到无穷大

var min = markArr.min();
var max = markArr.max();

document.write(min);
或者我们可以停止使用原型

这些不是好的原型定义;它们被拴在一个物体上!让他们改用
这个

Array.prototype.max = function() {
    return Math.max.apply(Math, this);
};

Array.prototype.min = function() {
    return Math.min.apply(Math, this);
};
除此之外,
max
在您调用它以获取
最新的
(它应该是
未定义的
)时不是一个函数,您也没有使用这些函数。我很惊讶你能得到无穷大

var min = markArr.min();
var max = markArr.max();

document.write(min);

或者我们可以停止使用原型

您需要传递数组本身。如果在数组上调用了该方法,则该方法中的该方法将是该数组,因此:

Array.prototype.max = function() {
  return Math.max.apply(Math, this);
};
对其他方法也要这样做


请注意,扩展Array.prototype将影响数组上枚举中的..,因此需要进行hasOwnProperty测试以筛选出原型上的可枚举属性。

您需要传递数组本身。如果在数组上调用了该方法,则该方法中的该方法将是该数组,因此:

Array.prototype.max = function() {
  return Math.max.apply(Math, this);
};
对其他方法也要这样做


请注意,扩展Array.prototype将影响数组上枚举中的..,因此需要进行hasOwnProperty测试以筛选出原型上的可枚举属性。

您需要传递数组本身。如果在数组上调用了该方法,则该方法中的该方法将是该数组,因此:

Array.prototype.max = function() {
  return Math.max.apply(Math, this);
};
对其他方法也要这样做


请注意,扩展Array.prototype将影响数组上枚举中的..,因此需要进行hasOwnProperty测试以筛选出原型上的可枚举属性。

您需要传递数组本身。如果在数组上调用了该方法,则该方法中的该方法将是该数组,因此:

Array.prototype.max = function() {
  return Math.max.apply(Math, this);
};
对其他方法也要这样做


请注意,扩展Array.prototype将影响数组上枚举中的..,因此需要进行hasOwnProperty测试以筛选出原型上的可枚举属性。

Infinity
在使用非整数时也会引发问题

然后,最好在调用
max
之前调用
map

即:
当您将
Math.max
与非整数一起使用时,也会出现
Infinity
问题

然后,最好在调用
max
之前调用
map

即:
当您将
Math.max
与非整数一起使用时,也会出现
Infinity
问题

然后,最好在调用
max
之前调用
map

即:
当您将
Math.max
与非整数一起使用时,也会出现
Infinity
问题

然后,最好在调用
max
之前调用
map

即:

你想在这里做什么:
Math.min.apply(Math,markArr)
-将markArr与Math进行比较?@cale\u b:这需要
markArr
的最小元素。你应该使用确认而不是提示,这样你就可以得到一个布尔响应。我想得到用户输入数组的最小值,但它也显示无穷大为什么?如何解决这个问题?
var max=max(markArr)你不是这样做的。你想在这里做什么:
Math.min.apply(Math,markArr)
-将markArr与Math进行比较?@cale\u b:这将采用
markArr
的最小元素。您应该使用确认而不是提示,这样您可以得到布尔响应。我想得到用户输入数组的最小值,但它也显示无穷大为什么?如何解决这个问题?
var max=max(markArr)你不是这样做的。你想在这里做什么:
Math.min.apply(Math,markArr)
-将markArr与Math进行比较?@cale\u b:这将采用
markArr
的最小元素。您应该使用确认而不是提示,这样您可以得到布尔响应。我想得到用户输入数组的最小值,但它也显示无穷大为什么?如何解决这个问题?
var max=max(markArr)你不是这样做的。你想在这里做什么:
Math.min.apply(Math,markArr)
-将markArr与Math进行比较?@cale\u b:这将采用
markArr
的最小元素。您应该使用确认而不是提示,这样您可以得到布尔响应。我想得到用户输入数组的最小值,但它也显示无穷大为什么?如何解决这个问题?
var max=max(markArr)你不是这样做的。它仍然显示无穷大,但我该如何摆脱它?@HelloWorld:你使用JSFIDLE了吗?获得无穷大的唯一方法是使用空数组。我?