I think the first if statement is correct, Stefan, since we must be sure that the numbers array actually contains at least one number. So, numbers.length must indeed be strictly greater than 0.
What is not correct is...
1) The initial variable declaration:
var numbers = (1,2,3); // You can't use parentheses to declare an array without using the array keyword like this:
//var numbers = new Array(1,2,3);
// Instead, you have to declare an array by using the short square brackets notation:
var numbers = [1,2,3];
2) The arguments passed to the max() and the min() methods of the Math object can only be a list of numbers separated by commas, but not an array of numbers.
So, to use it on an array, we first need to reduce this array...
// ... a first time to get the maximum value:
var maxValue = numbers.reduce(function(a, b) {
return Math.max(a, b);
});
// ... a second time to get the minimum value:
var minValue = numbers.reduce(function(a, b) {
return Math.min(a, b);
});
And only then you substract the two previously obtained values.
var differenceValue = maxValue - minValue;
You function becomes:
The text editor doesn't allow me to write the overall body of the mostNumbers() function, so I attached a text file to my reply for that.
mostNumbers(numbers); // We call the function to test it in the browser.
(Note: in addition to the initial if statement, we should check that all the values included in the numbers array only contain actual numbers, and otherwise display a message that warns that at least one member of the array is not a number.)
One_quick_solution.txt