Guest User

scoring

a guest
Jan 13th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1.  
  2.  
  3. var candidateNames = ["Benjamin", "Donald", "George", "John", "Rutherford"];
  4. function getCandidates() {
  5. var first_scores = [0,0,0,0,0];
  6. var ranked_scores = [0,0,0,0,0];
  7. var ballots = window.location.hash.split("#")[1].split(",");
  8. console.log(ballots);
  9. for (var i = 0; i < ballots.length; i++) {
  10. var ballot = ballots[i];
  11. first_scores[ballot[0]] += 1;
  12. for (var j = 0; j < ballot.length; j++) {
  13. ranked_scores[ballot[j]] += 5 - j;
  14. }
  15. }
  16.  
  17. // Extract actual first from firsts
  18. var first_max = -1;
  19. var first_idx = -1;
  20. for (var i = 0; i < first_scores.length; i++) {
  21. if (first_scores[i] > first_max) {
  22. first_max = first_scores[i];
  23. first_idx = i;
  24. }
  25. }
  26. console.log(first_idx);
  27. console.log(candidateNames[first_idx]);
  28.  
  29. // Extract actual ranked max
  30. var ranked_max = -1;
  31. var ranked_max_idx = -1;
  32. for (var i = 0; i < ranked_scores.length; i++) {
  33. if (ranked_scores[i] > ranked_max) {
  34. ranked_max = ranked_scores[i];
  35. ranked_max_idx = i;
  36. }
  37. }
  38.  
  39. console.log(ranked_max);
  40. console.log(candidateNames[ranked_max_idx]);
  41. }
  42.  
  43. getCandidates()
Advertisement
Add Comment
Please, Sign In to add comment