I have declared Array prototype myArrayMax and the code seems right after repeated verifications from other resources on w3schools.com :
The code is for finding the maximum number in a given array.
Yet, it doesn't work, and the console shows the following error message:
Uncaught TypeError: points.myArrayMax is not a function
I can't find the problem in this prototype declaration
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points.myArrayMax();
var len, max;
Array.prototype.myArrayMax = function() {
len = this.length;
max = -Infinity;
while (len--) {
if (this[len] > max) {
max = this[len];
}
}
return max;
}
<h2>JavaScript Array Sort</h2>
<p>The highest number is <span id="demo"></span>.</p>
Because you're attempting to use Array.myArrayMax
before you actually define it.
var points = [40, 100, 1, 5, 25, 10];
var len, max;
Array.prototype.myArrayMax = function() {
len = this.length;
max = -Infinity;
while (len--) {
if (this[len] > max) {
max = this[len];
}
}
return max;
}
console.log(points.myArrayMax());