Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getNameWeight(name) {
  2.   return name.length + name.split("").reduce(function(res, char) {
  3.     return res + char.toUpperCase().charCodeAt() - "A".charCodeAt() + 1;
  4.   }, 0);
  5. }
  6.  
  7. function rank(st, we, n) {
  8.     if (!st) {
  9.       return "No participants";
  10.     }
  11.    
  12.     var results = [];
  13.     st.split(",").forEach(function(name, index) {
  14.       results.push({"name": name, "weight": getNameWeight(name)*we[index]});
  15.     });
  16.     if (n > results.length) {
  17.       return "Not enough participants";
  18.     };
  19.     results.sort(function(a, b) {
  20.       return (b.weight - a.weight) || a.name.localeCompare(b.name);
  21.     });
  22.     return results[n-1].name;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement