# Grade student as number of array scores

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 ){

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

}
</script>

• 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. – jfriend00 May 12 '16 at 6:43
• Hi @jfriend00 : thanks for the comment. I would like to grade students as number(1, 2, 3, 4, 4, 6...) of array scores. – Rady Archuleta May 12 '16 at 11:23
• I have rolled back the last edit. Please see what you may and may not do after receiving answers. – Quill May 12 '16 at 11:27

var position   = 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>');


That should give you the following result...

Grade: 1<br>


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


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

• 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. – mdfst13 May 12 '16 at 8:30
• Hi @ponsfrilus: thanks for the answer but look at the result, 2,3,3,3,6,8... it doesn't work hik hik – Rady Archuleta May 12 '16 at 11:16
• That's why I said that you can adjust the Maths. Anyway how can you give same score different grade ? That's absurd... @RadyArchuleta – ponsfrilus May 12 '16 at 18:03

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);