初学程序员在将JavaScript与案例陈述联系起来时遇到问题?

时间:2013-12-04 03:04:05

标签: javascript html

我是一名初学程序员,他对此代码有困难,并希望得到任何反馈或帮助。此代码的目的是创建一个主要顾问,可以计算最适合放置哪所学校。我的编码问题是提交按钮没有连接而且case语句没有连接。请给我任何建议和任何反馈,你认为我可以做些什么来使这个正常工作。谢谢您的帮助。

<html>
<head>
<title>Major Adviser</title>
</head>
<body>
<script type=text/javascript>

function Calculate(a, b, c, d) {

var C = 0;
var SB = 0;
var S = 0;
var M = 0;
var SM = 0;
var LA = 0;
var GP = 0; 

function a(){
switch (document.ZF.elements.Hobbies)
  {
  case value=="Math":
    S + 2;
    SM + 3;
    LA + 1;
    break;
  case value=="Science":
    C + 2;
    SB + 2;
    M + 2;
    LA + 1;
    GP + 2;
    break;
  case value=="Computer Club":
SM + 1;
    C + 1;
    SB + 1;
    M + 2;
    LA + 2;
    GP + 1;
    break;
  case value=="Television Club":
    SM + 1;
    C + 1;
    SB + 1;
    M + 2;
    LA + 2;
    GP + 1;
    break;
  case value=="Community Service":
    SM + 1;
    C + 1;
    SB + 1;
    M + 2;
    LA + 2;
    GP + 1;
    break;
  case value=="Music":
    SM + 1;
    C + 1;
    SB + 1;
    M + 2;
    LA + 2;
    GP + 1;
    break;
case value=="Environmentalist":
    SM + 1;
    C + 1;
    SB + 1;
    M + 2;
    LA + 2;
    GP + 1;
break;
case value=="Acting":
    SM + 1;
    C + 1;
    SB + 1;
    M + 2;
    LA + 2;
    GP + 1;
break;
default:
    document.writeln("not working");
    }
}

function b(){

    switch (document.ZF.elements.Subject);
  {
  case value=="Math":
    S + 2;
    SM + 3;
    LA + 1;
    break;
  case value=="Science":
        C + 2;
        SB + 2;
       M + 2;
         LA + 1;
        GP + 2;
    break;
  case value=="Social Studies":
        SM + 1;
        C + 1;
        SB + 1;
        M + 2;
        LA + 2;
        GP + 1;
    break;
  case value=="English":
        SM + 1;
        C + 1;
        SB + 1;
        M + 2;
        LA + 2;
        GP + 1;
    break;
  case value=="Computers":
        SM + 1;
        C + 1;
        SB + 1;
        M + 2;
        LA + 2;
        GP + 1;
    break;
  case value=="Art":
        SM + 1;
        C + 1;
        SB + 1;
        M + 2;
        LA + 2;
        GP + 1;
    break;
default:
    document.writeln("not working");
    }
}

function c(){

switch (document.ZF.elements.type_of_person);
  {
  case value=="Introvert":
        S + 2;
        SM + 3;
        LA + 1;
    break;
  case value=="Extrovert":
        C + 2;
        SB + 2;
        M + 2;
        LA + 1;
        GP + 2;
    break;
  case value=="Both":
        SM + 1;
        C + 1;
        SB + 1;
        M + 2;
        LA + 2;
        GP + 1;
    break;
default:
    document.writeln("not working");
        }
}

function d(){

document.writeln("School of Communication and Arts: " + C);
document.writeln("School of Social and Behavioural Science: " + SB);
document.writeln("School of Science: " + S);
document.writeln("School of Science and Mathematics: " + SM);
document.writln("School of Management: " + M);
document.writln("School of Liberal Arts: " + LA);
document.writln("School of Global and Professional Programs: " + GP);

    }
    }
    </script>

<h1 align="right"><img src="Marist_College_2_220374.JPG" width="15%" height="15%" alt="Marist Logo"> <img src="poughkeepsie1.JPG" width="45%" height="15%" alt="Poughkeepsie"/> <img src="Marist_College_2_220374.JPG" width="15%" height="15%"       alt="Marist Logo"/>
</h1>

<h3 style="text-align:center">Marist Major Advisor</h3>
<form name="ZF">
First Name: <input type="text" name="firstname" size = 25 maxlength = 15> <br>
<br>
Last Name: <input type="text" name="lastname" size = 25 maxlength = 15> <br>
<br>
<p>What kind of Personality are you?</p>
<input type="radio" name="type_of_person" value="Introvert">Introvert<br>
<input type="radio" name="type_of_person" value="Extrovert">Extrovert<br>
<input type="radio" name="type_of_person" value="Both">Both<br>
<input type="radio" name="type_of_person" value="None">None<br>
<p>What are your best subjects/activities?</p>
<input type="checkbox" name="Subject" value="Math">Math<br>
<input type="checkbox" name="Subject" value="Science">Science<br>
<input type="checkbox" name="Subject" value="Social Studies">Social Studies<br>
<input type="checkbox" name="Subject" value="English">English<br>
<input type="checkbox" name="Subject" value="Computers">Computers<br>
<input type="checkbox" name="Subject" value="Art">Art<br>
<p>What activities or hobbies are you involved in?</p>
<input type="checkbox" name="Hobbies" value="Math">None<br>
<input type="checkbox" name="Hobbies" value="Science">Sports<br>
<input type="checkbox" name="Hobbies" value="Computer Club">Computer Club<br>
<input type="checkbox" name="Hobbies" value="Television Club">Television Club<br>
<input type="checkbox" name="Hobbies" value="Community Service">Community Service<br>
<input type="checkbox" name="Hobbies" value="Music">Music<br>
<input type="checkbox" name="Hobbies" value="Environmentalist">Environmentalist<br>
<input type="checkbox" name="Hobbies" value="Acting">Drama Class/Acting<br>
<p>How long are you willing to be in college for?</p>
<select>
    <option value="blank"></option>
    <option value="Don't Mind">Don't mind the amount of time.</option>
    <option value="2Year">2 years Associates Degree</option>
    <option value="4Year">4 years Bachelors Degree</option>
    <option value="4+">4+ Masters Degree</option>
    <option value="Doctrine">Doctrine</option>
</select><br>
<br>
<input type="button" value="Submit" onclick="Calculate()"/>
<input type="reset"Value="Clear">
</form>
</body>
</html>

4 个答案:

答案 0 :(得分:2)

由于您不熟悉编程,我需要指出几件事。

如果您打开任何现代浏览器并访问开发人员工具,则可能会遇到语法错误。例如,如果您在Firefox中,只需右键单击并选择Inspect Element with Firebug。这将让你自己开始。

所以这里有修复:

  • 您需要<!DOCTYPE html>开头。

  • 所有作业都需要从S + 2;更改为S += 2;。否则你需要做S = S +2;

  • 之类的事情
  • 如上所述,您可以从案例陈述中删除value ==

  • 如上所述,您有writln而不是writeln

  • switch语句末尾有几个分号,不能存在。例如switch (document.ZF.elements.Subject);

  • 对于“提交”按钮,我将type="button"更改为type="submit",取代onclick代替jQuery .submit()事件。

  • switch语句没有使用value,它们只会查看第一个选中的语句。因此,需要for循环来提取checked值,并一次将它们传递给切换函数1。

我还清理了它的副本中的格式和语法结构,并添加了一些jQuery。

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <meta charset="ISO-8859-1">
    <title>Major Adviser</title>
</head>
<body>
    <script type=text/javascript>
    $( document ).ready( function() {
        var C,SB,S,M,SM,LA,GP;

        $("#ZF").submit(function() {
            C = 0;
            SB = 0;
            S = 0;
            M = 0;
            SM = 0;
            LA = 0;
            GP = 0;

            var hobbies = $('.hobbies');
            for(var i = 0; i < hobbies.length; i++) {
              if (hobbies[i].checked) {
                var hobby;
                hobby = hobbies[i].value;
                a(hobby);
              }
            }

            var subjects = $('.subjects');
            for(var i = 0; i < subjects.length; i++) {
              if (subjects[i].checked) {
                var subject;
                subject = subjects[i].value;
                b(subject);
              }
            }

            c();
            d();
        });

        var a = function(hobby) {
            switch (hobby) {
            case "Math":
                S += 2;
                SM += 3;
                LA += 1;
                break;
            case "Science":
                C += 2;
                SB += 2;
                M += 2;
                LA += 1;
                GP += 2;
                break;
            case "Computer Club":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "Television Club":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "Community Service":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "Music":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "Environmentalist":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "Acting":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            default:
                document.writeln("not working");
            }
        };

        var b = function(subject) {
            switch (subject) {
            case "Math":
                S += 2;
                SM += 3;
                LA += 1;
                break;
            case "Science":
                C += 2;
                SB += 2;
                M += 2;
                LA += 1;
                GP += 2;
                break;
            case "Social Studies":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "English":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "Computers":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            case "Art":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            default:
                document.writeln("not working");
            }
        };

        var c=  function() {

            switch ($('.type_of_person').val()) {
            case "Introvert":
                S += 2;
                SM += 3;
                LA += 1;
                break;
            case "Extrovert":
                C += 2;
                SB += 2;
                M += 2;
                LA += 1;
                GP += 2;
                break;
            case "Both":
                SM += 1;
                C += 1;
                SB += 1;
                M += 2;
                LA += 2;
                GP += 1;
                break;
            default:
                document.writeln("not working");
            }
        };

        var d=  function() {

            document.writeln("School of Communication and Arts: " + C);
            document.writeln("School of Social and Behavioural Science: " + SB);
            document.writeln("School of Science: " + S);
            document.writeln("School of Science and Mathematics: " + SM);
            document.writeln("School of Management: " + M);
            document.writeln("School of Liberal Arts: " + LA);
            document.writeln("School of Global and Professional Programs: " + GP);

        };
    });
    </script>

    <h1 align="right">
        <img src="Marist_College_2_220374.JPG" width="15%" height="15%" alt="Marist Logo"> 
        <img src="poughkeepsie1.JPG" width="45%" height="15%" alt="Poughkeepsie" /> 
        <img src="Marist_College_2_220374.JPG" width="15%" height="15%" alt="Marist Logo" />
    </h1>

    <h3 style="text-align: center">Marist Major Advisor</h3>
    <form name="ZF" id="ZF">
        First Name: <input type="text" name="firstname" size=25 maxlength=15>
        <br> <br> 
        Last Name: <input type="text" name="lastname" size=25 maxlength=15> 
        <br> <br>
        <p>What kind of Personality are you?</p>
        <input type="radio" name="type_of_person" class="type_of_person" value="Introvert">Introvert<br>
        <input type="radio" name="type_of_person" class="type_of_person" value="Extrovert">Extrovert<br>
        <input type="radio" name="type_of_person" class="type_of_person" value="Both">Both<br>
        <input type="radio" name="type_of_person" class="type_of_person" value="None">None<br>
        <div id='subject-div'>
            <p>What are your best subjects/activities?</p>
            <input type="checkbox" name="Subject" class="subjects" value="Math">Math<br>
            <input type="checkbox" name="Subject" class="subjects" value="Science">Science<br>
            <input type="checkbox" name="Subject" class="subjects" value="Social Studies">Social Studies<br> 
            <input type="checkbox" name="Subject" class="subjects" value="English">English<br>
            <input type="checkbox" name="Subject" class="subjects" value="Computers">Computers<br>
            <input type="checkbox" name="Subject" class="subjects" value="Art">Art<br>
        </div>
        <div id='hobbies-div'>
            <p>What activities or hobbies are you involved in?</p>
            <input type="checkbox" name="Hobbies" class="hobbies" value="Math">Math<br>
            <input type="checkbox" name="Hobbies" class="hobbies" value="Science">Science<br>
            <input type="checkbox" name="Hobbies" class="hobbies" value="Computer Club">Computer Club<br> 
            <input type="checkbox" name="Hobbies" class="hobbies" value="Television Club">Television Club<br> 
            <input type="checkbox" name="Hobbies" class="hobbies" value="Community Service">Community Service<br> 
            <input type="checkbox" name="Hobbies" class="hobbies" value="Music">Music<br>
            <input type="checkbox" name="Hobbies" class="hobbies" value="Environmentalist">Environmentalist<br>
            <input type="checkbox" name="Hobbies" class="hobbies" value="Acting">Drama Class/Acting<br>
        </div>
        <p>How long are you willing to be in college for?</p>
        <select>
            <option value="blank"></option>
            <option value="Don't Mind">Don't mind the amount of time.</option>
            <option value="2Year">2 years Associates Degree</option>
            <option value="4Year">4 years Bachelors Degree</option>
            <option value="4+">4+ Masters Degree</option>
            <option value="Doctrine">Doctrine</option>
        </select><br> <br> 
        <input type="submit" value="Submit"/> 
        <input type="reset" Value="Clear">
    </form>
</body>
</html>

答案 1 :(得分:2)

有许多不好的事情在继续:

  • 您的案例陈述应为case "Math":
  • switch行末尾没有分号。
  • 您的增量应为S += 2SM += 3
  • 您错误地拼写了writeln几次。
  • 鉴于您如何使用CSB等变量,它们应该是全球性的。
  • 函数嵌套在Calculate中。删除所有这些内部功能。
  • 您测试价值的方式不起作用。如果您使用的是现代浏览器,请尝试使用document.querySelector("[name=Subject]").value和其他类似的浏览器。
  • 这在技术上并不重要,但你的变量名称是残暴的。我的意思是仁慈。

答案 2 :(得分:1)

case value=="Computers":

应该是:

case "Computers":

等。 您不需要value==部分。

此外,你有像这样的行中的拼写错误:

document.writln("School of Management: " + M); // writeln

答案 3 :(得分:1)

Here's a working example to help you get started。请注意,版本有一些修改,可以使调试更容易。

以下是导致脚本崩溃的原因:

  • case语句不使用case value=="Math"之类的比较。以下是正确的格式

    switch(n) {
      case 1:
        // execute code block
        break;
      case 2:
        // execute code block
        break;
      default:
        // code to execute if n is different from case 1 and 2
     }
    

    除此之外,你还没有定义valuevalue == 'Whatever'会引发ReferenceError

  • 为了增加变量的值,您需要重新分配新值,如下所示:

    var x = 0;
    x + 1;     // `x` is still 1
    x = x + 2; // `x` is now 2
    x += 2;    // Shorthand for previous assignment; `x` is now 4
    x++;       // Even shorter-hand for `x += 1`; `x` is now 5
    

    因此S + 2;之类的语句不会更改S的值,您需要使用上面提到的其中一种形式(例如S += 2)来更改S的值Calculate

  • 您似乎误解了应该如何使用参数(a.k.a.参数)。您的a函数实际上并不需要参数bcdfunction a()以及您定义的后续方法({{1}无法通过将值作为参数传递给Calculate来运行。只是为了澄清论证如何运作,这里是一个快速回顾。与数学函数非常相似,值被传递给JavaScript函数以在其中使用。考虑这个简单的multiply函数:

    function multiply(x, y) {
      return x * y;
    }
    multiply(7, 3) // returns 21
    

    这里我们发送了两个要在计算中使用的值。

  • 如果您仍希望将每个switch语句保存在函数中,则需要稍后调用该函数。例如:

    function greet(name) {
      function sayHello() {
        console.log('Hello ' + name + '!');
      }
      function askQuestion() {
        console.log('How are you ' + name + '?');
      }
      sayHello();
      askQuest();
    }
    

    虽然JavaScript确实允许在函数中嵌套函数,但只应明智地进行,例如when defining a class。这个例子不太合格。

  • document.ZF.elements.Whatever返回NodeList个对象,而不是checked元素的值。在提取值之前,您需要在列表中指定一个元素:     var hobbies = document.ZF.elements.Hobbies;     var hobby = hobbies [0] .value

  • 如果您想累积多个复选框项目的分数,您需要迭代NodeList中的所有元素,同时通过分数计算功能。

    var hobbies = document.ZF.elements.Hobbies;
    for(var i = 0; i < hobbies.length; i++) {
      if (hobbies[i].checked) {
        var hobby;
        hobby = hobbies[i].value;
        calculateHobbyScore(hobby);
      }
    }
    
  • 您错误地将writeln拼错了writln几次!

  • 您应该在浏览器中使用JavaScript控制台。它可以帮助您更轻松地捕获代码中的错误。您可以使用键盘快捷键CTRL + Shift + J

    访问Chrome和Firefox中的JavaScript控制台

    Chrome Console

    Assertion failure example

    Firefox Console

    enter image description here