1

In my program, I'm getting grades from students using the and calculating their grade. to check what grade they get I have listed the grades data in a dictionary and I want to check if my grade is bigger than certain grade give them A if not it'll continue until it finds the proper grade. Thus, I have arranged my dictionary objects to be from highest grade to the lowest, so once it finds the proper grade stop and prints the grade to the user. for some reason it is looping from the lowest key object to the highest key, how would I fix that? to loop from the first key to the last no matter what's its value.

var dict = {94: '"It is, A, excellent"', 90: '"It is, A-, great job"', 87: '"It is, B+, great"', 84: "It is, B, amazing", 80: "It is, B-, study little more", 77: "It is, C+, you could do better", 74: "It is, C", 70: "It is, C-", 67: "Is your grade, D+"}
 function gradeCheck(sum){
            var tex;
            Object.keys(dict).forEach( function(key){
                //console.log(key, dict[key]);
                /*if(sum>=key && (sum-key)<6 ){
                    tex = dict[key];
                    return tex;
                }*/
                if(sum > key){
                    tex = dict[key];
                    return tex;
                }
            });
        }

I commented my old part code.

I hope someone can help, Thanks

  • 3
    Related: [Does ES6 introduce a well-defined order of enumeration for object properties?](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties). In this case the order of keys returned via `Object.keys` is not well defined and if you need to count on a specific ordering you should sort the array yourself before iterating it. – CRice Oct 08 '18 at 23:35
  • I don't think using numbers as keys on an object is the best way. I would use an array. – Adrian Brand Oct 08 '18 at 23:57

2 Answers2

2

Because JS objects donot preserve the order of insertion of keys. Use MAP instead of normal object.

var dict = new Map();
dict.set(94, 'It is, A, excellent');
dict.set(90, 'It is, A-, great job');
dict.set(87, '"It is, B+, great"');

function gradeCheck(sum) {
    var tex;
    for (var [key, value] of dict) {
        if (sum > key) {
            tex = value;
            console.log(tex);
            return tex;
        }
    }

} 
rohitwtbs
  • 509
  • 5
  • 17
1

Use an array

var grades = [
  { grade: 94, message: 'It is, A, excellent' },
  { grade: 90, message: 'It is, A-, great job' },
  { grade: 87, message: 'It is, B+, great' },
  { grade: 84, message: 'It is, B, amazing' },
  { grade: 80, message: 'It is, B-, study little more' },
  { grade: 77, message: 'It is, C+, you could do better' },
  { grade: 74, message: 'It is, C' },
  { grade: 70, message: 'It is, C-' },
  { grade: 67, message: 'Is your grade, D+' }
];

function gradeCheck(sum) {
 return grades.find(g => g.grade < sum).message;
}

console.log(gradeCheck(88));

console.log(gradeCheck(75));
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60