0

在 udemy 上关于 JavaScript 的在线课程。试图对我学到的东西进行编码。我收到一个未定义的错误。Visual Studio 2012 Pro 说代码已找到,但是当我运行代码时出现错误。

        <script type="text/javascript">
        var numericalGrade = 82;
        var letterGrade;
        function myResaults() {
            document.write("Your score is " + numericalGrade + "%. Your grade will be a " + letterGrade + ".<br />");
        }

        if (numericalGrade >= 90) {
            letterGrade = "A";
            document.write(myResaults() + " Excellent, you passed this course with flying colors...");
        }

        else if (numericalGrade >= 80) {
            letterGrade = "B";
            document.write(myResaults() + " Excellent, you passed this course with a great grade..");

        }
        else if (numericalGrade >= 70) {
            letterGrade = "C";
            document.write(myResaults() + " Congratulations, you passed this course...");

        }
        else if (numericalGrade >= 60) {
            letterGrade = "D";
            document.write(myResaults() + " You revived a grade that will not permit you to pass this course. You can retake this course at a later date.");

        }
        else {
            letterGrade = "F";
            document.write(myResaults() + "You failed this course. You can retake this course at a later date.");

        }
    </script>

那么我做错了什么。我对 JavaScript 编码非常陌生。

4

1 回答 1

3

您的函数需要返回一个值,但因为它不是您得到未定义的。

改变:

function myResaults() {
    document.write("Your score is " + numericalGrade + "%. Your grade will be a " + letterGrade + ".<br />");
}

function myResaults() {
    return "Your score is " + numericalGrade + "%. Your grade will be a " + letterGrade + ".<br />";
}
于 2013-08-16T20:10:16.643 回答