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

1

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, that was the problem. Actually your response is correct according to my question :D
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);

Comments

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?

Comments

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.

Comments

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.

5 Comments

Math.max() doesn't take an array as an argument. It takes one or more individual arguments.
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.
@JonathanSampson - Corrected the answer. But.. i was always under the impression that apply only dictates the values of this inside the function.
@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..
.apply() both sets the value of this and turns an array into successive arguments passed to the function which is what you needed here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.