Please explain how it works, and what does this code do?

function topSalary(salaries) {

 let max = 0;
 let maxName = null;

 for(const [name, salary] of the Object.entries(salaries)) {
 if (max < salary) {
 max = salary;
 maxName = name;

 return maxName;
April 4th 20 at 13:20
1 answer
April 4th 20 at 13:22
For example
 'John': 10000,
 'Emily': 11000,
 'Bob': 9200,
 'Alice': 11010
}); // Alice
. Next, max for numbers, maxName for key in object. Object.entries(salaries) this code from the object makes an array of arrays, like this:
[['John', 10000], ['Emily', 11000], ['Bob', 9200], ['Alice', 11010]]
. Part const [name, salary] of ... is gestructureerde assignment, ie, name gets the key, and salary number (in this case). Well, after the usual search algorithm maximum number and the return key (maxName).
the usual algorithm
and unclear to me. - Ricky_Champlin87 commented on April 4th 20 at 13:25
@Ricky_Champlin87, Well, in the first iteration, max = 0, and salary equal to 10000. The condition max < salary returns true, so the max is written to 10000, and in maxName - John. On the second and later iterations, a similar situation occurs. On the third iteration the situation changes, because max for the time equal to 11,000, and salary - 9200, this condition is not triggered and the value of variables max and maxName does not change.

Hope became clearer. - Gertrude.Tremblay32 commented on April 4th 20 at 13:28
Yes thanks for the explanation. It has now become a little clearer. Because I'm new to JS and programming in General, I'm not familiar with the algorithms, and therefore often have difficulty in problem solutions - Ricky_Champlin87 commented on April 4th 20 at 13:31

Find more questions by tags JavaScriptArrays