extending array function getting this err "Uncaught TypeError: Object [object Array] has no method 'max'"

toy*_*toy 0 javascript

hello im trying to build this function into my code:

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

Array.min = function (array) {
    return Math.min.apply(Math, array);
};
Run Code Online (Sandbox Code Playgroud)

as dictated by: JavaScript: min & max Array values?. However when i try to call it using:

console.log(a.max());
Run Code Online (Sandbox Code Playgroud)

where

a = [245, 3, 40, 89, 736, 19, 138, 240, 42]
Run Code Online (Sandbox Code Playgroud)

I get the following Error:

Uncaught TypeError: Object [object Array] has no method 'max' 
Run Code Online (Sandbox Code Playgroud)

Can someone help me with this?

the*_*eye 5

You have to add those functions in the prototype, not on the Array object itself.

Array.prototype.max = function () {
...

Array.prototype.min = function () {
...
Run Code Online (Sandbox Code Playgroud)

Apart from that, to make your program work, you have to make the following changes

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

Array.prototype.min = function() {
    return Math.min.apply(null, this);
};
Run Code Online (Sandbox Code Playgroud)

You want to call those functions on the current Array object, so, you have to use this variable instead of accepting an array as a parameter.