JavaScript Checking loop for match most efficiently

I have a simple set up to find top scores. The goal is to order who received the highest scores and then give the top 3 bonus points.

Question: In part two below I am looping through the people and checking who got the top score. The issue is I need to do this 3 times for each name. The way I currently have the loop set up works but seems very redundant. Is this the correct way to do it?

   var allGrades = [
        {name: 'andrew', score: 80}, 
        {name: 'john', score: 90}, 
        {name: 'sam', score: 31}, 
        {name: 'frank', score: 50}, 
        {name: 'dave', score: 95},
        ]

    allGrades.sort(function(obj1, obj2) {
    return obj2.score - obj1.score;
     });

Part two. Giving the top 3 scoring people a bonus of 50/25/15 accordingly.

for (i = 1; i < 5; i++) {

if (allGrades[0].name == 'andrew'){
andrewGradeBonus = 50; 
if (allGrades[1].name == 'andrew'){
andrewGradeBonus = 25;
if (allGrades[2].name == 'andrew'){
andrewGradeBonus = 15;

if (allGrades[0].name == 'dave'){
daveGradeBonus = 50; 
if (allGrades[1].name == 'dave'){
daveGradeBonus = 25;
if (allGrades[2].name == 'dave'){
daveGradeBonus = 15;

continued for all names...

Answer:1

You're on the right track, but if you're already sorting, there is no need to loop again. You already know the three persons with the highest scores which are are at index 0, 1 and 2. I'd just do the following:

 var allGrades = [
        {name: 'andrew', score: 80}, 
        {name: 'john', score: 90}, 
        {name: 'sam', score: 31}, 
        {name: 'frank', score: 50}, 
        {name: 'dave', score: 95},
        ]

    allGrades.sort(function(obj1, obj2) {
    return obj2.score - obj1.score;
     });


allGrades[0]["bonusPoints"] = 50; 
allGrades[1]["bonusPoints"] = 25; 
allGrades[2]["bonusPoints"] = 15; 

Therefore, allGrades would look like this:

[{name:"dave",score:95,bonusPoints:50},
 {name:"john",score:90,bonusPoints:25}, 
 {name:"andrew",score:80,bonusPoints:15},
 {name:"frank",score:50}, 
 {name:"sam",score:31}] 

As you can see, in the result above, you know dave have a 95 score with 50 bonus points. andrew 90 with 25 bonus points, and andrew 80 with 15 bonus points. The rest have no bunus points.

Answer:2

You could just iterate over the grades and increase the actual score:

const gradeBonuses = [50, 25, 15];
allGrades.sort((a, b) => b.score - a.score).forEach((grade, index) => {
    if (index < 3) {
        grade.score += gradeBonuses[index];
    }
});

If you really want the separate bonus prop, just switch grade.score += gradeBonuses[index]; with

const bonusProp = `${grade.name.toLowerCase()}GradeBonus`
       grade[bonusProp] = gradeBonuses[index];

But seeing as the object already has the name stored in it, why do you need to add the name to the name of your bonus property as well?

Answer:3

I have basically this structure for my data (this.terms): { name: 'First Category', posts: [ { name: 'Jim James', tags: [ 'nice', 'friendly' ...

I have basically this structure for my data (this.terms): { name: 'First Category', posts: [ { name: 'Jim James', tags: [ 'nice', 'friendly' ...

During development of a vue project, and got some doubt regarding template / render / VNode. After reading the document https://vuejs.org/v2/guide/syntax.html, and google search, still didn't ...

During development of a vue project, and got some doubt regarding template / render / VNode. After reading the document https://vuejs.org/v2/guide/syntax.html, and google search, still didn't ...

  1. what is a relationship template

I have the following object: productDetails: { cislife: [], prime: [] } I want to return null instead of empty array if there aren't any values available. e.g: I want to check if any of ...

I have the following object: productDetails: { cislife: [], prime: [] } I want to return null instead of empty array if there aren't any values available. e.g: I want to check if any of ...

  1. loop through object javascript
  2. loop through object keys javascript
  3. loop through object properties
  4. loop through object typescript
  5. loop through object keys
  6. loop through object properties c#
  7. loop through object c#
  8. loop through object array javascript
  9. loop through object react
  10. loop through object es6
  11. loop through object php
  12. loop through object java
  13. loop through object lodash
  14. loop through object angular
  15. loop through object jquery
  16. loop through object python
  17. loop through object javascript es5
  18. loop through object javascript key value
  19. loop through object values
  20. loop through object jsx

So I'm trying to make a function that grabs a HTML element and then after I've selected the HTML element I try to apply CSS to it via JavaScript but it doesn't seem to work. Instead, I get an error ...

So I'm trying to make a function that grabs a HTML element and then after I've selected the HTML element I try to apply CSS to it via JavaScript but it doesn't seem to work. Instead, I get an error ...

  1. javascript function that returns a function
  2. javascript function that returns a value
  3. javascript function that returns a promise
  4. javascript function that does nothing
  5. javascript function that returns boolean
  6. javascript function that accepts array
  7. javascript function that returns array
  8. javascript function that returns an object
  9. javascript function that can be suspended and resumed
  10. javascript function that adds numbers
  11. javascript function that returns nothing
  12. javascript function that only runs once
  13. javascript function that calls another function
  14. javascript function that runs on page load
  15. javascript function that executes immediately
  16. javascript function that takes a string
  17. javascript function that adds two numbers
  18. javascript function that returns html
  19. javascript function that takes an array
  20. javascript function that always runs