简体   繁体   中英

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"); } } }

You could consider to take a function for getting a grade out of a given percent value.

This function features an early return, because if a value is found the function could be terminated without testing other values.

This pattern prevents to use a continuing if ... else if ... else if ... pattern, because only false condition goes on with checking.

 function getGrade(percent) { if (percent >= 90) { return 'A'; } if (percent >= 80) { return 'B'; } if (percent >= 70) { return 'C'; } if (percent >= 60) { return 'D'; } return 'F'; } console.log(getGrade(90)); // A console.log(getGrade(81)); // B console.log(getGrade(72)); // C console.log(getGrade(63)); // D console.log(getGrade(54)); // F

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);

Have a look on this:

<!DOCTYPE html>
    <html>
    <body>

    <p>Click the button to demonstrate the prompt box.</p>

    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    <script>


    function myFunction() {

    var gradeData = prompt('Enter your grade to check your letter grade');
    var grade=getGrade(gradeData);
    alert(grade);
    }
    function getGrade(gradeData) {
        if (gradeData >= 90) {
            return 'A';
        }
        if (gradeData >= 80) {
            return 'B';
        }
        if (gradeData >= 70) {
            return 'C';
        }
        if (gradeData >= 60) {
            return 'D';
        }
        return 'F';
    }

    </script>

    </body>
    </html>

The other way you can modify your code like the following:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to type in your Marks and get grades.</p>

<button onclick="gradeCalculator()">Click here</button>

<p id="demo"></p>

<script>
var nameData = ['A', 'B', 'C', 'D', 'F'];   

function gradeCalculator() {
var gradeData = prompt('Enter your grade to check your letter grade');
getAvg(nameData,gradeData);
}

function getAvg(nameData, gradeData) {        
        if (gradeData>= 90) {
          alert("Your grades are " + nameData[0]);
        } else if (gradeData >= 80) {
          alert("Your grades are " + nameData[1]);
        } else if (gradeData >= 70) {
          alert("Your grades are " + nameData[2]);
        } else if (gradeData >= 60) {
          alert(" Your grades are " + nameData[3]);
        } else {
          alert("Your grades are " + nameData[4]);
        }

    }
</script>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM