在javascript中使用数组

时间:2013-06-06 04:41:53

标签: javascript arrays

嗨,我是一名初学程序员,我刚开始学习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>

2 个答案:

答案 0 :(得分:1)

这是因为你在一个函数中有2个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');

答案 1 :(得分: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。

希望这有帮助!