1

嗨,我是一名初级程序员,我刚开始学习 javascript。我正在尝试构建一个简单的 javascript 程序,该程序将提示用户从候选人列表中选择一个名称,然后选择他们想要接收信息的类别。然后程序应该显示该信息。我编写了下面列出的代码,但由于某种原因它不起作用,我无法弄清楚。任何帮助,将不胜感激。谢谢

<html>
<head> 
<title>interndatabase</title>
<script type="text/javascript"> 
//<!CDATA[ 

//stores data about the applicants
  applicantName= new array ("Joe","Sarah", "Roger", "Mike"); 
  applicantCategory= new array ("University","Year","SAT","GPA"); 
  applicantInfo= new array ( 
  new array ("Stanford","Senior","2250","3.6"), 
  new array ("UC Berkeley","Junior","2100","3.9"),
  new array ("MIT","Junior","2200","3.3"), 
  new array ("Carnegie Mellon","Sophomore","2150","3.4") 
     ); 

   //this function should evaluate said data 

     function getInfo (){ 
     var theApplicant=" "; 
     var menuA="Please choose an applicant by typing a number\n";
         menuA+="0)Joe\n"; 
         menuA+="1)Sarah\n"; 
         menuA+="2)Roger\n"; 
         menuA+="3)Mike\n"; 

         theApplicant=prompt(menuA); 
         return theApplicant; 

         var theCategory=" "; 
         var menuB="Please Choose a category by typing a number\n"; 
            menuB+="0)University\n"; 
            menuB+="1)Year\n"; 
            menuB+="2)SAT\n"; 
         menuB+="3)GPA\n"; 

     theCategory=prompt(menuB); 
     return theCategory;
      }//end function 

     //main code evaluates the result, and returns the correct info to the user 

      function main () { 
      var output=" "; 
      var name=getInfo() 
      var category=getInfo() 
      var result=applicantInfo [name] [category]; 

      output="The database belonging to" +applicantName; 
      output+="registers" +result+ "in that category."; 

      alert(output);
      }//end main

      </script> 
      </head>
      <body> 
      </body> 
      </html>
4

2 回答 2

1

这是因为你有二合一return的功能。你应该有2个独立的功能

function getApplicant(){ 
 var theApplicant=" "; 
 var menuA="Please choose an applicant by typing a number\n";
     menuA+="0)Joe\n"; 
     menuA+="1)Sarah\n"; 
     menuA+="2)Roger\n"; 
     menuA+="3)Mike\n"; 

     theApplicant=prompt(menuA); 
     return theApplicant; 
    }
 function getCategory(){ 
     var theCategory=" "; 
     var menuB="Please Choose a category by typing a number\n"; 
        menuB+="0)University\n"; 
        menuB+="1)Year\n"; 
        menuB+="2)SAT\n"; 
     menuB+="3)GPA\n"; 

 theCategory=prompt(menuB); 
 return theCategory;
  }

 //main code evaluates the result, and returns the correct info to the user 

  function main () { 
  var output=" "; 
  var name=getApplicant(); 
  var category=getCategory() ;
  var result=applicantInfo [name] [category]; 

  output="The database belonging to" +applicantName; 
  output+="registers" +result+ "in that category."; 

  alert(output);
  }//end main

您可以创建一个如下所示的数组:

applicantName = ['A','B','C'];

或者

applicantName  = new Array('A','B','C');
于 2013-06-06T04:52:35.340 回答
0

我已经尝试过这段代码并解决了这个问题。

创建新的 Javascript 数组时,它区分大小写。正确的代码是:

  var applicantName = new Array ("Joe","Sarah", "Roger", "Mike"); 
  var applicantCategory= new Array ("University","Year","SAT","GPA"); 
  var applicantInfo= new Array ( 
  new Array ("Stanford","Senior","2250","3.6"), 
  new Array ("UC Berkeley","Junior","2100","3.9"),
  new Array ("MIT","Junior","2200","3.3"), 
  new Array ("Carnegie Mellon","Sophomore","2150","3.4") 
     ); 

其中 Array 区分大小写。我建议您也使用 Firefox 的调试工具来帮助您。

您遇到的另一个问题是您实际上并没有启动代码。在你的身体标签中,放

<body onload = "main()"> 

这样,您实际上启动了您创建的 javascript。

希望这有帮助!

于 2013-06-06T04:57:33.570 回答