Web Analytics Made Easy -
StatCounter Help pulling up information off of Parallel Arrays by prompt method. - CodingForum

Announcement

Collapse
No announcement yet.

Help pulling up information off of Parallel Arrays by prompt method.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help pulling up information off of Parallel Arrays by prompt method.

    Hi, I'm a newbie to JavaScript and i need help on one part of my code. I have 3 parallel arrays with the variables being id, name, and grade. I have to prompt a user to type in an id number and it will pull up the id, name, and grade associated with that id number. I've gotten the prompt and a while loop to make sure the entered ID is a number. But i'm not sure what to do from here. I need to use a for loop to pull up the info together.

    Here's my code so far.
    Code:
    <script type="text/javascript">
    	var idnum=prompt("Please Enter ID","");
    
    	while(isNaN(idnum)){
    		alert("Sorry the entered ID is not a number. Please try again.");
    		idnum=prompt("Please Enter ID","");
    	}
    
    	
    	
    	for(var i=0; i<=4; i++){
    	}
    	
    		document.write("<table border='1'><tr>");
    		document.write("<td>ID:</td>");
    		document.write("<td>"+idnum+ "</td>");
    
    		document.write("<tr>");
    		document.write("<td>Work:</td>");
    		document.write("<td>"+name[i]+ "</td>");
    
    		document.write("<tr>");
    		document.write("<td>Grade:</td>");
    		document.write("<td>" +grade[i]+ "</td>");
    		document.write("</table></tr>");
    There are 5 values for each variable in the arrays. The name and grade variables are pulling up nothing.

  • #2
    Well, first of all, you can't use document.write on a page that is already loaded. If you do, you will COMPLETELY ERASE the contents of the page and replace it with what you write. And, yes, that means you will erase the JavaScript code that is doing the document.write!

    But aside from that, your code doesn't make sense.

    You do
    Code:
    for(var i=0; i<=4; i++){
    	}
    and so that means the loop *WILL* occur 5 times. And at the conclusion of the loop, i *will* be 5.

    So now when you try to do
    Code:
        grade[i]
    you are really doing
    Code:
        grade[5]
    And if your array only has 5 elements, they are numbered 0 through 4, so there isn't any element 5 and indeed you should get blank (or an error).

    I don't understand the purpose of the variable i at all, much less the use of the loop that ensures it is always 5.

    Oh...and what happens if the user enters "8371.33871" for the value of idnum? You code will happily accept that.

    So... need to rewrite this pretty much from scratch. Need to use something like document.getElementById( ) to get a reference to where you want to put the name[idnum] (see what I just wrote? that's a huge hint), et al.

    I'd suggest that you have the <table> already exist on the page, but hidden, and then you just un-hide it after filling in the values. The values could be put into <td> cells that have individual ID's.
    Be yourself. No one else is as qualified.

    Comment


    • #3
      Hey thanks man for what youve told me, like i said im just in a beginner class in college, and my teacher can barely speak english. So what i learn is what i teach myself.

      Comment

      Working...
      X