Functions and Arrays in Javascript.. Oh my!

Discussion in 'Programming' started by ibex333, Nov 19, 2012.

  1. ibex333

    ibex333 Diamond Member

    Joined:
    Mar 26, 2005
    Messages:
    3,430
    Likes Received:
    0
    I find this stuff very difficult to understand..

    first, I have to create an array of 9 elements, then I have to populate the array with numbers, and then I have to use a function that searches through the array and reports if number was found. I did all that, but the function never "triggers". It's as if I never wrote that code. What gives?

    Code:
     
    
    <html>
    	<head>
    		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	</head>
    	<body>
    		<script type="text/javascript">
    			/********* GLOBAL VARIABLES *********/
    			// Enter any global variable in this region
    			
    
    
    			/********* FUNCTIONS *********/
    			// Enter any functions in this region
    
    			function countInRow(array, searchVal)
    			{
    					for (i = 0; i < 10; i++)
    				{
    					if (boardArray[i] === searchVal)
    					{
    						document.write("Match not found in index" + i);
    					}
    					else
    					{
    						document.write("Match found in index" + i);
    
    					}
    					
    
    				}
    			
    			}
    
    			/********* MAIN *********/
    			function main()
    			{
    				// this will be the entry point of your program,
    				// write your main code here
    
    
    			
    				
    				
    			var SIZE = 9;	
    			var boardArray = new Array(SIZE); //give me 9 pieces of memory //literal 9 constant
    			var BR = "<br />";
    			var i;
    				for (i = 0; i < 10; i++)
    				{
    					boardArray[i] = prompt("Enter a number for box" + i); //we are populating the array one box at a time
    					document.write([i]); // to show which numbers we entered in the array
    				}	
    				
    			var searchVal = prompt("Enter the value you are looking for within the array");
    				
    				
    				
    				
    				
    				
    				
    				
    				
    				countInRow(boardArray, searchVal);
    
    				// This line calls main, don't change it:
    				
    			}
    			main();
    		</script>
    	</body>
    
    </html>
    
    
     
  2. sygyzy

    sygyzy Lifer

    Joined:
    Oct 21, 2000
    Messages:
    14,001
    Likes Received:
    1
    I think the reason it fails is in your function you ask for "function countInRow(array, searchVal)" but in the actual function you are using boardArray which it has no recognition of.

    Try changing if (boardArray === searchVal) to if (array === searchVal)
     
  3. ibex333

    ibex333 Diamond Member

    Joined:
    Mar 26, 2005
    Messages:
    3,430
    Likes Received:
    0
    YES! Perfect! Thank you. Except there is still one small issue.


    Code:
    document.write([i]); // to show which numbers we entered in the array
    this shows me the index numbers 0-9, but what I want is for it to show what is stored in indexes 0-9 after the prompt collects all input.
     
  4. KLin

    KLin Lifer

    Joined:
    Feb 29, 2000
    Messages:
    29,330
    Likes Received:
    3
    You're only passing in i into the write method, not the array variable.
     
  5. sygyzy

    sygyzy Lifer

    Joined:
    Oct 21, 2000
    Messages:
    14,001
    Likes Received:
    1
    Try document.write(boardArray);
     
  6. purbeast0

    purbeast0 Lifer

    Joined:
    Sep 13, 2001
    Messages:
    42,663
    Likes Received:
    46
    firebug ... download it.

    or press F12 in chrome.
     
  7. clamum

    clamum Lifer

    Joined:
    Feb 13, 2003
    Messages:
    23,691
    Likes Received:
    6

    This.

    Just a suggestion, instead of your "for" loop looking like this in function "countInRow()":

    Code:
    for (i = 0; i < 10; i++)
    I'd change it to:

    Code:
    for (i = 0; i < array.length; i++)
    That way you can easily change how many numbers are stored in the array and won't have a hard-coded limit. :)
     
  8. ibex333

    ibex333 Diamond Member

    Joined:
    Mar 26, 2005
    Messages:
    3,430
    Likes Received:
    0
    It does nothing for someone that doesn't understand how to use it. And even then, the messages it outputs dont really tell what the problem is in a clear way.





    Thanks. That did it.
     
  9. KLin

    KLin Lifer

    Joined:
    Feb 29, 2000
    Messages:
    29,330
    Likes Received:
    3
    That's why you download it, use it, use google to research errors you don't understand, etc. etc. Practice, practice, practice.
     
Loading...