-1

so I'm writing this very simple function and I'm trying to return it's result but all I get is NaN error.

Here is my current code:

function m(c) {
    return Math.max(c) - this.length - 1;
}


document.getElementById('app').innerHTML = m([1,4,6,8,43,2]);
<p id="app">

</p>

When I return typeof(a - this.length -1); instead, I get a number as a result.

    function m(c) {
        return typeof(Math.max(c) - this.length - 1);
    }


    document.getElementById('app').innerHTML = m([1,4,6,8,43,2]);
<p id="app">

</p>

So my question is why am I getting NaN error if type of my return statement is a number?

@edit Question number 2. Is there a way for me to change the function so that it displays the result without NaN error?

2
  • what is Math.max(c) supposed to do? Commented Mar 13, 2018 at 13:30
  • what is this? please add the wanted result as well. Commented Mar 13, 2018 at 13:32

4 Answers 4

3

NaN is of the number type:

console.log(typeof NaN)

The reason you're getting NaN is that you can't just call Math.max on an array. (Try Math.max([1,2])). You'll need to use the spread operator (...).

Also, I assume you meant to use the array's length, since this.length doesn't refer to anything.:

function m(c) {
    return Math.max(...c) - c.length - 1;
}


console.log(m([1,4,6,8,43,2]));

In case you need to support older browsers, replace Math.max(...c) with:

Math.max.apply(null, c)
Sign up to request clarification or add additional context in comments.

Comments

1

You have to use Math.max.apply(null, c). Math.max takes a list of numbers, not an array. That's why you have to use apply to use the array as a argument list.

1 Comment

Mdn docs: However, both spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters. See Using apply and built-in functions for more details. The reduce solution does not have this problem.
1

this.length is undefined. Replace it with c.length.

Also, math.max doesn't work as you think. If you look at the mdn docs you can see that they recommend a reduce function for arrays.

However, both spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters. See Using apply and built-in functions for more details. The reduce solution does not have this problem.

So in short.

function m(c) {
   
    var max = c.reduce(function(a, b) {
        return Math.max(a, b);
    });
    return max - c.length - 1;
}


document.getElementById('app').innerHTML = m([1,4,6,8,43,2]);
<p id="app">

</p>

1 Comment

Can the downvoter explain? The reduce function literally comes from: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
1

Because this context is not bound with that array, you can bind it as follow, further, you need to sort the array and get the max value.

function m() {
  var [max] = this.sort((a, b) => b - a);
  return max - this.length - 1;
}

document.getElementById('app').innerHTML = m.bind([1, 4, 6, 8, 43, 2])();
<p id="app"></p>

Or you can Spread the array:

function m(c) {
  return Math.max(...c) - c.length - 1;
}

document.getElementById('app').innerHTML = m([1, 4, 6, 8, 43, 2]);
<p id="app"></p>

1 Comment

sort to get the largest value? .bind to work around abuse of this? I'd suggest changing the OP's methodology instead of making their hacks "work".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.