1

I want to make a function that determines which day of the week i spent more money and if two or more days have the highest amount, an array containing the days should be returned or if the input is null or an empty array, the function should return null.

But i'm very confused. So far i've tried to write the function that sorts the array in descending order but it doesn't seem right:

highamount= [50, 100, 2];   

highamount.sort(function(amount[0],amount[1])) { 

return amount[1]-amount[0]; 
}

What is wrong? And what if i don't have a defined array before and just want to sort it when i call the function, what would be in the function then?

I'm new to Javascript and i need help.

KatyK
  • 17
  • 1
  • 4
  • 1
    did u saw this?: https://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array – Black.Jack Jan 28 '18 at 22:18
  • 1
    Possible duplicate of [How might I find the largest number contained in a JavaScript array?](https://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array) – Barış Uşaklı Jan 28 '18 at 22:19
  • Another short (ES2015) way: `Math.max(...highAmount)` returns `100`. – JJWesterkamp Jan 28 '18 at 22:35

3 Answers3

2

The problem with your code snippet

  • This line is incorrect highamount.sort(function(amount[0], amount[1])) because your closing your comparator function with an extra parenthesis.
    highamount.sort(function(amount[0], amount[1]))
                                                  ^
  • You're not declaring params in your compare function, you're accessing to an undefined object/array amount.
        function(amount[0], amount[1])
                 ^          ^
  • The logic of your compare function has a problem because you're accessing to an undefined object/array amount, this is similar to the previous problem.
        amount[1] - amount[0]
        ^           ^
  • You're not closing the sort function correctly, it's missing the closing parenthesis.
        return amount[1] - amount[0];
    };
    ^
  • Great, now let's analyze your logic.

Imagine your code has a correct syntax:

var highamount = [50, 100, 2];

highamount.sort(function(a, b) { 
  return a - b; 
});

console.log(highamount);

See? your logic it's Ok because the array highamount was sorted. Now you can get the max value executing: highamount[highamount.length - 1]

Better approach

Good, how we can improve your approach to find the max value from your array highamount.

The most recent Javascript version provides good capabilities to accomplish your scenario:

With that operator, you can literally spread your Array and pass its values as params to other functions, arrays, Etc.

So, you can do the following:

var highamount = [50, 100, 2];

var max = Math.max(...highamount);

console.log(max);

See? Amazing how Javascript provides a readable way to accomplish your scenario.

Now, you wrote: And what if I don't have a defined array before and just want to sort it when I call the function, what would be in the function then?

Final approach

Well, you can declare a function which receives an array to find the max value.

Run this code snippet:

var highamount = [50, 100, 2];

var findMax = function(array) {
  return array === undefined || array === null || array.length === 0 ? null : Math.max(...array);
}

console.log(findMax(highamount));
console.log(findMax(null));
console.log(findMax(undefined));
console.log(findMax([]));

See? the function findMax returns the max value according to the passed array.

Resources

halfer
  • 19,824
  • 17
  • 99
  • 186
Ele
  • 33,468
  • 7
  • 37
  • 75
1

You can use the ES2015 spread syntax and Math.max for a very short one-liner:

const numbers = [1, 5, 74, 27, 2, 8, 442, 12];
const largest = Math.max(...numbers); // > 442

The spread syntax passes all members of numbers as arguments to Math.max. Therefore, the following 2 statements are equivalent:

Math.max(...numbers);
Math.max(1, 5, 74, 27, 2, 8, 442, 12);
JJWesterkamp
  • 7,559
  • 1
  • 22
  • 28
0

The title question is different from the question you are ask in the body of the post, that's why no one else is giving you the right answer. You want an array of the indexes of days that have the highest value. And in either case, sorting is not necessary.

If you want an array of indexes of the highest values, ( or an array of Days, as you say). Then this is how I would do it:

if (highAmount.length === 0) {
    return null;
}
let highest = highAmount[0];
let indexes = [0];
for (var i = 1; i < highAmount.length; i++) {
    if (highest < highAmount[i]) {
        highest = highAmount[i];
        indexes = [i];
    }else if(highest = highAmount[i]){
        indexes.push(i);
    }
}
return indexes;

If you just want the highest value, all you have to do is iterate through the array.

if (highAmount.length === 0) {
    return null;
}
let highest = highAmount[0];
for (var i = 1; i < highAmount.length; i++) {
    if (highest < highAmount[i]) {
        highest = highAmount[i];
    }
}
return highest;

Or just use Math.max(...highAmount)

keithlee96
  • 1,724
  • 2
  • 11
  • 17
  • I want a function that determines the day with the highest amount and returns the day not the amount. And if 2 days have the same amount it should return the array with both the days. Where would the input for both the days and the amount be? Its very confusing. – KatyK Jan 28 '18 at 23:02
  • The function does what you want it to. It returns the array of days. – keithlee96 Jan 29 '18 at 02:10
  • and how do i call the function? i mean how's the input? – KatyK Jan 29 '18 at 10:50