0

I'm writing Javascript for the first time and am stuck. For this program, I am supposed to categorize the scores as A = 90 – 100, B = 80-89, C = 70 – 79, D = 60 -69 and F <60. Then print the message out that there are x A’s, x B’s, x C’s , x D’s and x F’s.where x is the amount of number of scores falling into the category. How would I go about this?

</body>
</html>

<!DOCTYPE html>
<head>
</head>
<body>
<script type = "text/Javascript"> 

      function testScores(){
      scores = ["65", "75", "99", "82", "77", "100", "75", "88", "100", "75" ];

      }
      function sortScores(){
      for(i=1; i<=9; i = i+1)
         document.write(scores[i] + " "); 

      }

</script>
<button type="button" onclick = "testScores();sortScores()"> Test Scores </button>

</body>
</html>
kap
  • 1
  • 1

3 Answers3

0

Loop through the array (note, they are numbers, so you don't need "), and increase a variable each time a number matches a filter - then print out the variables:

var a = 0;
    var b = 0;
    var c = 0;
    var d = 0;
    var f = 0;
    var arr = [65, 75, 99, 82, 77, 100, 75, 88, 100, 75];
    for (var i = 0; i < arr.length; i++) {
      if (89 < arr[i] && arr[i] <= 100) {
        a++;
      }
      if (79 < arr[i] && arr[i] <= 90) {
        b++;
      }
      if (69 < arr[i] && arr[i] <= 80) {
        c++;
      }
      if (59 < arr[i] && arr[i] <= 70) {
        d++;
      }
      if (arr[i] <= 59) {
        f++;
      }
    }


    document.getElementById('results').innerHTML=('A = ' + a + '<br>B = ' + b + '<br>C = ' + c + '<br>D = ' + d + '<br>F = ' + f + '<br>');
<div id="results"></div>
sideroxylon
  • 4,338
  • 1
  • 22
  • 40
  • In above solution - if OP comes with another grade and range It will be difficult and you have to add another `if condition` - which is not a proper solution – prog1011 Feb 19 '15 at 09:03
  • This is the simplest solution to the question as asked. We know nothing about future changes to requirements (which would, if they are as you propose, be easy to implement anyhow). – sideroxylon Feb 19 '15 at 09:06
0

Try This

  • You should use two-dimensional-array for this and it will be easy for you to future changes. It means if you have a new grade in future you jus have to add it with range and Grade Name like (a,b,c, ETC.)

View DEMO

function testScores() {
            try {
                    var fullNameArray = {
                        0: [90, 100, 0,"A"],
                        1: [80, 89, 0,"B"],
                        2: [70, 79, 0,"C"],
                        3: [60, 69, 0,"D"],
                        4: [0, 59, 0,"F"]
                    }
                var scores = ["65", "75", "99", "82", "77", "100", "75", "88", "100", "75"];
                var len = numProps(fullNameArray);
                for (var i = 0; i <= scores.length; i = i + 1) {

                    for ( var k = 0; k <= len; k++) {
                        if (scores[i] >= fullNameArray[k][0] && scores[i] <= fullNameArray[k][1]) {
                            
                            fullNameArray[k][2] = fullNameArray[k][2] + 1;
                        }
                    }

                }
                for (var D = 0; D <= len; D++) {                    
                    document.getElementById('diVID').innerHTML = document.getElementById('diVID').innerHTML+"<br/>"+fullNameArray[D][3] +" : "+ fullNameArray[D][2];
                    
                }
            }
            catch (err) {
                alert(err);
            }
    }

function numProps(obj) {
  var c = 0;
  for (var key in obj) {
    if (obj.hasOwnProperty(key)) ++c;
  }
  return c-1;
}
<input type="button"  value="Click OK" onclick="testScores()" />

<div id="diVID"></div>

JSFIDDLE Here

Community
  • 1
  • 1
prog1011
  • 3,425
  • 3
  • 30
  • 57
0

Here is a possible solution:

function testScores(){
    var scores = [65, 75, 99, 82, 77, 100, 75, 88, 100, 75];
    var boundaries = [90, 80, 70, 60];
    var points = sortScores(scores, boundaries);
    document.write(" >= " + (boundaries[0]) + ": " + points[0]);
    for (var i=1; i < boundaries.length; ++i) {
        document.write(", " + (boundaries[i-1]-1) + "-" + boundaries[i] + ": " + points[i]);
    }
    document.write(", < " + boundaries[boundaries.length-1] + ": " + points[boundaries.length]);
}

function sortScores(scores, boundaries) { 
    var points = [], group;
    for (group=0; group<=boundaries.length; ++group) {
        points[group] = 0;
    }
    for(i=0; i<scores.length; ++i) {
        group = 0;
        while (group < boundaries.length && scores[i] < boundaries[group]) {
            ++group;
        }
        ++points[group];
    }
    return points;
}

testScores();

Fiddle here: http://jsfiddle.net/robbyn/72ote3pm/

Maurice Perry
  • 32,610
  • 9
  • 70
  • 97