Answer the question
In order to leave comments, you need to log in
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
For example
topSalary({
'John': 10000,
'Emily': 11000,
'Bob': 9200,
'Alice': 11010
}); // Alice
. Next, max
for the digit, maxName
for 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 name
key gets into, and into salary
the 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 questionAsk a Question
731 491 924 answers to any question