1

I have this javascript code: http://jsfiddle.net/MvWV7/5/

What I'm trying to achieve is that user should fill the inputs starting with 1. After the user types 1 the next value must be 2 (not nanother number) and so on.

I'm trying to fill the inputs values in an array by doing this (as shown in the fiddle):

var ary = [];   
$(".activity_order").not(self).each(function(t) {ary.push(this.value);})

but when I do

ary.max()

I get

Uncaught TypeError: Object ,,,,,,,, has no method 'max' 

In console when I do ary.max() i get 0 if there's no numbers

UPDATE

My fault, I was using google console in jsfiddle and I started to look up for array methods inside. When I did ary.max() it gives 0

Trott
  • 66,479
  • 23
  • 173
  • 212
JavierQQ23
  • 704
  • 1
  • 8
  • 20

5 Answers5

3

You're presumably typing [].max into the console on the jsfiddle domain. Note the output:

function () {
    return Math.max.apply(null, this); // 'this' is your array
}

The reason here is that code on the jsfiddle domain has modified the prototype of Array to include the max method. It's mapped to the Math.max method and doesn't natively reside on the Array object itself, or its prototype.

Sampson
  • 265,109
  • 74
  • 539
  • 565
0

An array does not have a .max() method. If you want to create one, you will have to write or procure a function to iterate through an array and find the largest value in the array.

As it turns out, you can use the Math.max() function to solve this problem like this:

var m = Math.max.apply(null, ary);
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Array does not have a max method. But there is Math.max that you can use for this.

var m = Math.max.apply(0, ary);

The reason why you cannot call it like Math.max(ary) is that Math.max accepts a series of values instead of an array. Calling using apply, converts the second array argument into a sequence of arguments.

techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • `Math.max()` doesn't take an array as an argument. It takes one or more individual arguments. – jfriend00 Mar 27 '13 at 18:51
  • `Math.max` is a function, and as such its prototype has the `apply` method which does in fact accept an array. @jfriend00 You are technically in error here. I'm upvoting on that basis. – Sampson Mar 27 '13 at 18:52
  • @JonathanSampson - Corrected the answer. But.. i was always under the impression that `apply` **only** dictates the values of `this` inside the function. – techfoobar Mar 27 '13 at 18:53
  • @JonathanSampson - as you can see in the answer I supplied, there is a way to use `.apply()` with `Math.max()` but that turns an array into successive arguments which Math.max does accept. You can't pass an array like this: `Math.max(arr)` which is what I said and why this answer is wrong.. – jfriend00 Mar 27 '13 at 18:53
  • `.apply()` both sets the value of `this` and turns an array into successive arguments passed to the function which is what you needed here. – jfriend00 Mar 27 '13 at 18:54
0

The issue is that .max and .min don't exist. They aren't methods, just like the thrown error says.

You would have to modify the array prototype to do something in the syntax you provided.

This was already discussed here: JavaScript: min & max Array values?

Community
  • 1
  • 1
Jeff Shaver
  • 3,315
  • 18
  • 19
0

There is no max() and min() methods in array object natively. You should add them before trying to use it.

Array.prototype.min = function () {
  return this.reduce(function (p, v) {
    return ( p < v ? p : v );
  });
}

Array.prototype.max = function () {
  return this.reduce(function (p, v) {
    return ( p > v ? p : v );
  });
}

Here is working fork of your's code with this methods added.

Denis O.
  • 1,841
  • 19
  • 37