Drm2108 Drm2108 - 3 years ago 155
Javascript Question

javascript grade calculator using arrays and functions clarification

I am trying to write a program that will display the letter grade once user inputs a real or float number. Prompt question works but the function is not working. I tried debugging the code with no success.

Any help is welcomed. Thanks!



var nameData = ['A', 'B', 'C', 'D', 'E', 'F'];
var nameDataLength = nameData.length;
var avgGrade = 0;
var gradeDate = prompt('Enter your grade to check your letter grade');

function getAvg(nameData, gradeData) {
for (var i = 0; i < nameData.length; i++) {
alert("Your grades are " + nameData[i] + " ," + gradeData[i]);
if (gradeData[i] >= 90) {
alert("Your grades are " + nameData[i] + "A");
} else if (gradeData[i] >= 80) {
alert("Your grades are " + nameData[i] + "B");
} else if (gradeData[i] >= 70) {
alert("Your grades are " + nameData[i] + "C");
} else if (gradeData[i] >= 60) {
alert(" Your grades are " + nameData[i] + "D");
} else {
alert("Your grades are " + nameData[i] + "F");
}
}
}




Answer Source

You have several problems:

  • typo: nameDate -> nameData
  • you never call your function
  • your function does not do what you think it does

I will solve the first two so that you can see the third.

var nameData = ['A', 'B', 'C', 'D', 'E', 'F'];
var nameDataLength = nameData.length;
var avgGrade = 0;

function getAvg(gradeData) {
  for (var i = 0; i < nameData.length; i++) {
    alert("Your grades are " + nameData[i] + " ," + gradeData[i]);
    if (gradeData[i] >= 90) {
      alert("Your grades are " + nameData[i] + "A");
    } else if (gradeData[i] >= 80) {
      alert("Your grades are " + nameData[i] + "B");
    } else if (gradeData[i] >= 70) {
      alert("Your grades are " + nameData[i] + "C");
    } else if (gradeData[i] >= 60) {
      alert(" Your grades are " + nameData[i] + "D");
    } else {
      alert("Your grades are " + nameData[i] + "F");
    }
  }
}


var gradeData = prompt('Enter your grade to check your letter grade');
// you need to actually call the function.
getAvg(gradeData);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download