Mohammed Touati
Mohammed Touati

Reputation: 51

Function gives NAN instead of a string

i'm a beginner in JS and i have a problem with this function: first of all i defined it :

function highAndLow(numbers){
     numbers = numbers.replace(/ /g,",");
     let high = Math.max(numbers);
     let min = Math.min(numbers);
     console.log("The max value is "+high+ ", and the minimum is "+min);
};

and when i call it :

console.log(highAndLow("1 2 3 4 5"));

i get the following output :

The max value is NaN, and the minimum is NaN

So please could you show where is the problem and thanks in advance.

Upvotes: 1

Views: 724

Answers (5)

Shahzad
Shahzad

Reputation: 1705

The problem is Math.max expects a collection and you are passing a string which just contains the numbers. COnvert the collection into an array and pass it.

function highAndLow(numbers){
     numbers = numbers.replace(/ /g,",");
     var numbersList = numbers.split(",").map(function(num){
      return parseInt(num);
     });
     let high = Math.max(...numbersList);
     let min = Math.min(...numbersList);
     console.log("The max value is "+high+ ", and the minimum is "+min);
};

console.log(highAndLow("1 2 3 4 5"));

Upvotes: 1

Siri
Siri

Reputation: 1126

function highAndLow(numbers) {
     numbers = numbers.split(' ').map(n => +n);
     let high = Math.max(...numbers);
     let min = Math.min(...numbers);
     return "The max value is "+ high+ ", and the minimum is "+ min;
}

console.log(highAndLow("1 2 3 4 5"));

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138247

I think the main missunderstanding is that:

 Math.max("1,2,3")

and

  Math.max(1, 2, 3)

are equal (the first is what you do, the second is what you want). They are not as in the first case you pass a string to Math.max, while in the second you pass multiple numbers. To turn the string into an array of numbers, you can .split it into an array of strings first, then parse the strings into numbers. That array can then be spread as arguments:

  Math.max(..."1 2 3".split(" ").map(Number))

Upvotes: 1

Ghoul Ahmed
Ghoul Ahmed

Reputation: 4806

Try this

function highAndLow(numbers){
     numbers = numbers.split(" ");
     let high = Math.max(...numbers);
     let min = Math.min(...numbers);
     console.log("The max value is "+high+ ", and the minimum is "+min);
};


 highAndLow("1 2 3 4 5");

Upvotes: 1

Maheer Ali
Maheer Ali

Reputation: 36564

You are making three mistakes.

  • You are using replace() on string. Which will output a string. You can't pass string to Math.min/max. You should use split() by ' '
  • Even if you split() the elements of arrays will be strings. You can convert it to numbers using map()
  • Math.min/max doesn't expects array as argument. If you want to pass array use Spread Operator

function highAndLow(numbers){
     numbers = numbers.split(' ').map(Number)
     let high = Math.max(...numbers);
     let min = Math.min(...numbers);
     console.log("The max value is "+high+ ", and the minimum is "+min);
};

console.log(highAndLow("1 2 3 4 5"));

Upvotes: 1

Related Questions