在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编码的新手。
答案 0 :(得分: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 />";
}