We help IT Professionals succeed at work.

# How to write a function using array and sort() in JavaScript?

on
Hi, I need to write a function in Java Script that takes a score list of any length as a parameter, and returns the list of grades
associated with those scores.

For example, if there are following scores:

93, 93, 90, 88, 87, 87, 86, 85, 85, 85, 74, 72,61, 54

The first 4 scores ( 93, 93, 90, 88) would be an A, next 4  (87, 87, 86, 85) would be a B, next 4  (74, 72,61, 54) would be a C,
etc

there should be  a sort() function which will sort a
list of elements in an array or list.

thanks
Comment
Watch Question

## View Solution Only

Commented:
what's wrong with the inherent javascript sort() function?

http://www.w3schools.com/jsref/jsref_sort.asp

Commented:

One way to do this:
``````var scores = [87, 94, 81, 88, 72, 90, 91, 70, 65, 83];
// this is the sample data

var criteria = {'A': {minScore: 90},
'B': {minScore: 80},
'C': {minScore: 70},
'D': {minScore: 60},
'F': {minScore: 0}};
// this is what determines the letter grade

var returnGrades = function(scores) {
var grades = {};
for (grade in criteria) {
// initialize each letter grade
}
scores = scores.sort().reverse();    // this is optional
for (i=0; i< scores.length; i++) {
var score = scores[i];
for (grade in criteria) {
if (score >= criteria[grade].minScore) {
break;
}
}
}
}
``````

Running returnGrades(scores) would yield:
``````{
A:[94, 91, 90],
B:[88, 87, 83, 81],
C:[72, 70],
D:[65],
F:[]
}
``````
Software Engineer
BRONZE EXPERT

Commented:
Something like this perhaps?
``````<html>
<body>

<script type='text/javascript'>
function letterGrade( score ) {
if ( score > 89 ) {
result = 'A'
} else if ( score > 79 ) {
result = 'B'
} else if ( score > 69 ) {
result = 'C'
} else if ( score > 59 ) {
result = 'D'
} else {
result = 'F'
}
return result
}

var grades = []
var students = Math.floor( Math.random() * 10 ) + 10
for ( var i = 0; i < students; i++ ) {
grades[ i ] = Math.floor( Math.random() * 50 ) + 50
}
document.write( 'Before: ' + grades + '<br>' );
document.write( ' After: ' + grades.sort() + '<br>' );
for ( var i = 0; i < students; i++ ) {
}
document.write( 'Grades: ' + grades + '<br>' );
</script>

</body>
</html>
``````
Multitechnician
SILVER EXPERT
Distinguished Expert 2019
Commented:
Check this one :
``````function sortList(l) {
var r = {};
l.sort(function(a,b){ return b-a; });
for(var i=0;i<l.length;i++) {
if( r[String.fromCharCode(0x41 + parseInt(i/4))] == null) r[String.fromCharCode(0x41 + parseInt(i/4))] = [];
r[String.fromCharCode(0x41 + parseInt(i/4))].push(l[i]);
}
return r;
}
``````

test page : http://jsfiddle.net/52n5y/
Owner (Aidellio)
SILVER EXPERT
Most Valuable Expert 2015

Commented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
Multitechnician
SILVER EXPERT
Distinguished Expert 2019

Commented:
My answer fulfilled the requirements : http://jsfiddle.net/52n5y/
Multitechnician
SILVER EXPERT
Distinguished Expert 2019

Commented: