0

Hi i'm a beginning programmer, i just started learning javascript. I am trying to build a simple javascript program that will prompt the user to select a name from a list of candidates, and then select which category they want to receive information about. Then program should then display that information. I wrote the code listed below, but from some reason it isn't working and i can't figure out way. Any help would be appreciated. thanks

<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

2 Answers 2

0

It's because you have 2 return in one function . You should have 2 separate functions

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

And you can create an array like below :

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

or

applicantName  = new Array('A','B','C');
Sign up to request clarification or add additional context in comments.

Comments

0

I've experimented with this code and have fixed the problem.

When creating a new Javascript array, it's case sensitive. The correct code is:

  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") 
     ); 

Where Array is case sensitive. I'd recommend using Firefox's debugging tools to help you as well.

Another problem you have is that you are not actually starting the code. In your body tag, put

<body onload = "main()"> 

This way, you actually start the javascript you created.

Hope this helped!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.