javascript - 在 JavaScript 中使用数组

标签 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 个 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');

关于javascript - 在 JavaScript 中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16953804/

相关文章:

javascript - 如何获得 react 组件的 promise 值(value)?

javascript - Node.js 将每一行分成一个整数数组

javascript - Ng-Bootstrap 导航(选项卡)不显示任何选项卡的内容

c++ - 如何与数组 union 并保持可移植性?

javascript - 原型(prototype)中的函数声明 "pollute"是原型(prototype)吗?

c - 基本 C/K&R 2.7/无论定义如何,字符都显示为菱形?

python - list(numpy_array) 和 numpy_array.tolist() 之间的区别

php - PHP 是否(在内部)以不同方式处理数字索引数组?

javascript - 如何在数组的每个项目前面添加文本?

javascript - sqlexception消息显示导致页面错误