Math.min and Math.max methods with JS

I first wrote this code:

let numbers = (10, 15, 59, 20);
let a = Math.max(numbers);
console.log(a);

…which display 20 in the console. I’ve noticed that it always returns the last number of the numbers variable.

Then, i wrote this (I’ve found it on W3S and modify it a bit):

function myFunction() {
  var a = Math.min(5, 10);
  var b = Math.min(0, 150, 30, 20, 38);
  var c = Math.min(-5, 10);
  var d = Math.min(-5, -10);
  var e = Math.min(1.5, 2.5);
  console.log(a + "<br>" + b + "<br>" + c + "<br>" + d + "<br>" + e); 
}

…which returns nothing.

So my question is: Do the Math methods work on the code editor of FCC? Or there’s an error in my code?

Thanks for help guys. I really enjoy the community.

Just to explain what you’re seeing, because it does trip people up: I assume you meant to write that as an array, but because you didn’t, the reason you’re getting 20 every time:

  • wrapping code in brackets like (10, 15, 59, 20) just tells JS to evaluate what is in the brackets.
  • commas are operators in JS, and the comma operator says “evaluate each value in turn and return the last one”
  • so that piece of code evaluates those numbers in turn: 10 is 10, 15 is 15, 59 is 59, 20 is 20. 20 is the last value, so that gets returned and assigned to numbers.
  • so the value of numbers is always 20. And Math.max(20) is always 20.

Thanks for your reply!

Thank you too for your reply! The title of my post is certainly a bit confusing and didn’t let enough space to an error of my part. I’ll try to not do the same mistake next time.

A last question: What return statement should I write so?