dimanche 21 juin 2020

JS. How do I call a function that iterates through an array?

Disclaimer: this is a homework assignment. I'm not sure if I have set up my code right. Is this the correct way to call a function that iterates through an array? The document write is not showing anything.

var main = new Array ( );
main[0] = new Array (90, 91, 92, 93, 94, 95, 96, 97, 98, 99);
main[1] = new Array (80, 81, 82, 83, 84, 85, 86, 87, 88, 89);
main[2] = new Array (70, 71, 72, 73, 74, 75, 76, 77, 78, 79);
main[3] = new Array (60, 61, 62, 63, 64, 65, 66, 67, 68, 69);
main[4] = new Array (50, 51, 52, 53, 54, 55, 56, 57, 58, 59);

//function declared
function getGrade(x) {
  var grade
  //inside the function, if/else statement
  if (x >= 90) {
    grade = "A";
  } else if (x >= 80) {
    grade = "B";
  } else if (x >= 70) {
    grade = "C";
  } else if (x >= 60) {
    grade = "D";
  } else {
    grade = "F";
  }
  return grade
};

// for loop 
for (i = 0; i < main.length; i++) {
  for (j = 0; j < main[i].length; j++) {
    document.write("David's number grade is" + main[i][j] + "and letter grade is a " + Getgrade(main[i]));
 }
};

Aucun commentaire:

Enregistrer un commentaire