Hey everybody, I'm a beginner at JavaScript so there are many things I don't understand. I'd appreciate very much if you can help me figuring out something new.
I'm trying to solve this problem on Checkio.org called "The Most Numbers". The requirement is quite simple: It is given an array of floats and you have to calculate the difference between the maximum and minimum numbers. And this is my solution (which didn't work):
function mostNumbers(numbers){
if (numbers.length > 0) {
return Math.max(numbers) - Math.min(numbers);
} else {
return 0;}
}
It produced "0" regardless of what "numbers" is.
I tried some stuff related to the Math.max() and Math.min() methods. And this is what I received:
Math.max(1,2,3) // => 3
But when I assigned the array to a variable, this is what I received:
var numbers = (1,2,3);
Math.max(numbers) // => 3 (nothing wrong)
Math.min(numbers) // => 3 (why?)
I realized that it always take the last number, regardless what I want, so I tried typing (1,2,3) into the console and I got 3. Why is that? Then I realized that this is not a normal array, which has the structure [something, something else,...].
I reached out to w3school but what I received are just simple examples like
Math.max(5, 10); // => 10
I reached out to stackoverflows as well and found this:
With all the respect for this person (who is contributing value, that's great), I find the problem even more confusing.
- What is a series of numbers, if not an array of numbers?
- What is a spread operator? I clicked on the link he/she provided, but I didn't understand what I read:
Thank you for reading my post, I know it's long. I appreciate it.