调试帮助(javascript初学者)

时间:2016-02-17 23:07:49

标签: javascript

这是我的代码,我正在为一个班级工作。任何帮助或建议都将非常感谢。无法弄清楚它为什么不起作用。

(我使用它作为填充因为它说我的帖子主要是代码,需要更多细节)

<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>School Year?</title>
<script type="text/javascript">
/*  Input: Name and Credits
*   Process: Use if command (keyword) and logic operaters to calculate school year.
*   Output: The student’s name and year in school.
*/

function schoolyear(){
var name = document.getElementById("name").value ;
var credits = document.getElementById("credits").value ;
var c = parseFloat(credits);

if (c >= 0 && c < 30) {
    year = "freshmen";
} 
else if (c >= 30 && c < 60) {
    year = "sophomore";
}
else if (c >= 60 && c < 90) {
    year = "junior";
}
else (c >= 90) {
    year = "senior";
}   
    document.getElementById("name").innerHTML;
    document.getElementById("outputDiv").innerHTML = year;

}
</script>
</head>

<body>

<h1> Find out what year of school your in based on credits. </h1>
Please input your name <input type ="text" id= "name"></br>
Please input your amount of credits earned <input type ="text" id= "credits"></br>
<button type = onclick="schoolyear()"> Find out </button></br>
<div id="outputDiv"></div>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

所以在我完成代码后,我看到了两个错误:

<button type= onclick="schoolyear()"> Find out </button></br>

删除&#34;输入=&#34;

<button onclick="schoolyear()"> Find out </button></br>

并从

更改此内容
else (c >= 90) {
year = "senior";}   

else if (c >= 90) {
year = "senior";}   

提示:无需定义&#34;信用卡&#34;两次,你可以把它缩短到这个;

var c = document.getElementById("credits").value;