Small script not working : s (Javascript)

Discussion in 'JavaScript' started by DFORMS, Jul 26, 2010.

  1. #1
    My script is not working can someone explain to me how to fix this?
    
    <html>
    	<head>
    		<title>Untitled Document</title>
    	</head>
    	<body>
    		<script language="javascript">
    			
    			Nom = new Array(3)
    			
    			Nom[0] = "Justin"
    			Nom[1] = "Michel"
    			Nom[2] = "Pascal"
    
    	
    			  function addName(tbl, name){
    			
    			    tbl = Nom;
    			    name = "";
    			
    			    name = prompt();
    			
    			    tbl_addName = tbl.push(name);
    
    			    }
    			
    			function showName(){
    			
    			  tbl_show = tbl_addName;
    			
    			  return tbl_show
    			 
    			  }
    						
    addName();
    aler(tbl_show);
    
    		
    		</script>
    	</body>
    </html>
    
    Code (markup):
    Thank you!
     
    DFORMS, Jul 26, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    But can you explain, how it has to work? :)
     
    s_ruben, Jul 27, 2010 IP
  3. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Since this is obviously a homework question, I will give hints and not answers :D

    How is the showName function suppose to run if nothing calls it?
    Have a closer look at aler(tbl_show); What's missing there?

    For bonus points, find out why language="javascript" is no longer used.
     
    Cash Nebula, Aug 3, 2010 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    If I understand correctly you are prompting the user for a name, which will then be added to the existing array of names, which will then be display. Is this correct? If so there is a few things wrong here...
    It is not necessary but is good practice to declare variables by using the 'var' statement. Also, not necessary but to end each statement with a semi-colon ; is also a better practice. So to start:
    
                            [B]var[/B] Nom = new Array([B]2[/B]);  //Take note that when defining the [I]upper bound[/I] of an Array (in this case 2), it should always be one less then the actual length of the array.
    			
    			Nom[0] = "Justin";
    			Nom[1] = "Michel";
    			Nom[2] = "Pascal";
    
    Code (markup):
    All the rest of the code can be simplified much easier, firstly the addName function only needs one parameter which is 'firstname'. You can then directly modify the original array using the same push method that you used before. So Nom.push(firstname); is all that needs to be within the addName function.
    Then the showname function simply needs to display the values of the Array ('Nom'). So alert(Nom); is all that is required for that function:
    
                function addName(firstname){
    			    Nom.push(firstname);
                }
    	   function showName(){
    			    alert(Nom);
    	   }
    
    Code (markup):
    This now comes all together very easily:
    
                addName(prompt());
                showName();
    
    Code (markup):
    The first line asks the user for there input, which is sent to the 'addName' function as a parameter.
    The showName() function then executes and displays the resulting array.

    A simpler version of this complete script would be:
    
                var Nom = ["Justin", "Michel", "Pascal"];
    	    Nom.push(prompt());
    	    alert(Nom);
    
    Code (markup):
     
    Last edited: Aug 3, 2010
    camjohnson95, Aug 3, 2010 IP