G
G
gorwhoami2020-01-22 14:00:14
JavaScript
gorwhoami, 2020-01-22 14:00:14

Can you please explain how it works and what this code does?

function topSalary(salaries) {
  
    let max = 0;
    let maxName = null;
  
    for(const [name, salary] of Object.entries(salaries)) {
      if (max < salary) {
        max = salary;
        maxName = name;
      }
    }
  
    return maxName;
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-01-22
@gorwhoami

For example

topSalary({
  'John': 10000,
  'Emily': 11000,
  'Bob': 9200,
  'Alice': 11010
}); // Alice
. Next, maxfor the digit, maxNamefor the key in the object. Object.entries(salaries)this code from an object makes an array of arrays, like this:
. Part of const [name, salary] of ...this is a destructuring assignment , i.e. the namekey gets into, and into salarythe number (in this case). Well, then the usual algorithm for finding the maximum number and returning the key ( maxName).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question