0
\$\begingroup\$

Assume that you have the ready sorted array

Is it a good solution?

Plunker OR:

<script>
    var score = [10, 20, 20, 20, 40, 50, 60, 60, 60, 60, 70, 80, 80, 90]
    var j = 0;
    var k = 0;

    for( i in  score ){
        var grade = '';

        if( i== 0 || score[i] > score[i - 1] ){
            grade = j = j+1+k;
            k = 0;
        }else{
            k++;
            grade = j;
        }

        document.write( 'Grade: ' + ( grade ) +'<br>');
    }
    </script>
\$\endgroup\$
  • 2
    \$\begingroup\$ What is the objective of this code? What are you trying to accomplish? For starters, you should never iterate an array with for( i in score ) as that iterates properties, not just array elements which can include things other than array elements. \$\endgroup\$ – jfriend00 May 12 '16 at 6:43
  • \$\begingroup\$ Hi @jfriend00 : thanks for the comment. I would like to grade students as number(1, 2, 3, 4, 4, 6...) of array scores. \$\endgroup\$ – Rady Archuleta May 12 '16 at 11:23
  • \$\begingroup\$ I have rolled back the last edit. Please see what you may and may not do after receiving answers. \$\endgroup\$ – Quill May 12 '16 at 11:27
1
\$\begingroup\$

The value your looking for is more like a position rather then a grade. Just add...

var position   = 0; 
var last_grade = 0; 

outside the for loop. then add a comparison before the document.write... statement like so.

if (grade>last_grade) position++;
document.write( 'Grade: ' + ( position ) +'<br>');
last_grade=grade;

That should give you the following result...

Grade: 1<br>
Grade: 2<br>
Grade: 2<br>
Grade: 2<br>
Grade: 3<br>
Grade: 4<br>
Grade: 5<br>
Grade: 5<br>
Grade: 5<br>
Grade: 5<br>
Grade: 6<br>
Grade: 7<br>
Grade: 7<br>
Grade: 8<br>
\$\endgroup\$
0
\$\begingroup\$

By reading your code it seems that you want to have grades between 0 and 15. Thus I do not understand why your are checking the previous entry in the array (score[i] > score[i - 1]), I think it can be converted to something more simpler and readable.

This is a proposal:

<script>
  var score = [10, 20, 20, 20, 40, 50, 60, 60, 60, 60, 70, 80, 80, 90];
  score.forEach(function(item) {
    document.write('Grade: ' + (Math.round(item/100*15)) + '<br>');
  });
</script>

Result:

Grade: 2
Grade: 3
Grade: 3
Grade: 3
Grade: 6
Grade: 8
Grade: 9
Grade: 9
Grade: 9
Grade: 9
Grade: 11
Grade: 12
Grade: 12
Grade: 14

If needed, you can easily change the Maths to suits your need.

\$\endgroup\$
  • \$\begingroup\$ Hi. Welcome to Code Review! While alternative coding solutions are good, we prefer to have more review in our answers. What was wrong with the original code? Why is this code better? How do you know that it returns the same result? It's better to help people learn how to code than to just give them code. \$\endgroup\$ – mdfst13 May 12 '16 at 8:30
  • \$\begingroup\$ Hi @ponsfrilus: thanks for the answer but look at the result, 2,3,3,3,6,8... it doesn't work hik hik \$\endgroup\$ – Rady Archuleta May 12 '16 at 11:16
  • \$\begingroup\$ That's why I said that you can adjust the Maths. Anyway how can you give same score different grade ? That's absurd... @RadyArchuleta \$\endgroup\$ – ponsfrilus May 12 '16 at 18:03
0
\$\begingroup\$

Your OP and your plunkr differ, in that the plunkr has the scores sorted from highest to lowest. Since that makes more sense to me, I'll assume that is correct.

I'd suggest a rewrite where you map each raw score to its rank, which is the heart of what you are doing:

var scores = [90, 80, 80, 70, 60, 60, 60, 60, 50, 40, 20, 20, 20, 10];
var grades = scores.map(x => scores.indexOf(x) + 1);

Which will produce:

[1, 2, 2, 4, 5, 5, 5, 5, 9, 10, 11, 11, 11, 14]

What this is saying is that each person's rank is equal to index, within the raw scores, of the first occurrence of their raw score.

Here's an updated plunkr

UPDATE

To answer your question in the comments you can do something like (this is untested, but should work):

objArr.sort((a,b) => b.projection - a.projection)
      .map(x => {
        var grade = objArray.findIndex(y => y.projection == x.projection) + 1);
        return Object.assign(x, {grade});
      });
\$\endgroup\$
  • \$\begingroup\$ Very nice Jonah... \$\endgroup\$ – Rady Archuleta May 16 '16 at 3:54
  • \$\begingroup\$ what about doing it over object with this algorithm [{"Id":571,"UserId":"auth0|56e7ea6f2698a49f50589a3f","Nickname":"sophorn","Entry":1,"Bets":59,"Payout":0,"projection":15479,"grade":1},"Id":470,"UserId":"auth0|567816fc203da0ab0da65bcf","Nickname":"khen","Entry":1,"Bets":8,"Payout":0,"projection":10859,"grade":2},{"Id":606,"UserId":"auth0|56e8e05f2698a49f5058b40d","Nickname":"rinrady","Entry":1,"Bets":1,"Payout":0,"projection":10229,"grade":3}]. I would like to give the player position by field "projection", can you help? \$\endgroup\$ – Rady Archuleta May 16 '16 at 4:18
  • \$\begingroup\$ not sure i understand, can you clarify? \$\endgroup\$ – Jonah May 16 '16 at 4:19
  • \$\begingroup\$ To grade player with below object by "projection" field [{"Id":571,"UserId":"auth0|56e7ea6f2698a49f50589a3f","Nickname":"sophorn","Entry‌​":1,"Bets":59,"Payout":0,"projection":15479,"grade":1},"Id":470,"UserId":"auth0|5‌​67816fc203da0ab0da65bcf","Nickname":"khen","Entry":1,"Bets":8,"Payout":0,"project‌​ion":10859,"grade":2},{"Id":606,"UserId":"auth0|56e8e05f2698a49f5058b40d","Nickna‌​me":"rinrady","Entry":1,"Bets":1,"Payout":0,"projection":10229,"grade":3}] \$\endgroup\$ – Rady Archuleta May 16 '16 at 4:25
  • \$\begingroup\$ I updated my answer with a solution. \$\endgroup\$ – Jonah May 16 '16 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.