Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Home and Garden
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 11-19-2012, 09:01 PM   #1
ibex333
Diamond Member
 
ibex333's Avatar
 
Join Date: Mar 2005
Location: Ukraine, Odessa - New York City, Brooklyn ; )
Posts: 3,263
Default Functions and Arrays in Javascript.. Oh my!

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>
__________________
2500k Sandy Bridge @ 4.40GHz(Yay!)~GIGABYTE Z68A-D3H-B3~G.SKILL 8GB DDR3 1333~
3.14 Tb HD Space~2x PNY GTX 760 GTX SLi~HT Omega Claro~Cooler Master CM690~600w OCZ StealthXtream~ASUS 27" Monitor
HEATWARE
EBAY
ibex333 is offline   Reply With Quote
Old 11-19-2012, 09:05 PM   #2
sygyzy
Lifer
 
sygyzy's Avatar
 
Join Date: Oct 2000
Posts: 14,001
Default

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[i] === searchVal) to if (array[i] === searchVal)
__________________
Peace!
Sygyzy
joshspear
The Perfect Cup of Tea (How-To)
sygyzy is offline   Reply With Quote
Old 11-19-2012, 09:39 PM   #3
ibex333
Diamond Member
 
ibex333's Avatar
 
Join Date: Mar 2005
Location: Ukraine, Odessa - New York City, Brooklyn ; )
Posts: 3,263
Default

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.
__________________
2500k Sandy Bridge @ 4.40GHz(Yay!)~GIGABYTE Z68A-D3H-B3~G.SKILL 8GB DDR3 1333~
3.14 Tb HD Space~2x PNY GTX 760 GTX SLi~HT Omega Claro~Cooler Master CM690~600w OCZ StealthXtream~ASUS 27" Monitor
HEATWARE
EBAY
ibex333 is offline   Reply With Quote
Old 11-19-2012, 09:46 PM   #4
KLin
Lifer
 
KLin's Avatar
 
Join Date: Feb 2000
Location: Phreaznaux
Posts: 28,770
Default

You're only passing in i into the write method, not the array variable.
__________________
'L_'
KLin is online now   Reply With Quote
Old 11-19-2012, 09:50 PM   #5
sygyzy
Lifer
 
sygyzy's Avatar
 
Join Date: Oct 2000
Posts: 14,001
Default

Try document.write(boardArray[i]);
__________________
Peace!
Sygyzy
joshspear
The Perfect Cup of Tea (How-To)
sygyzy is offline   Reply With Quote
Old 11-20-2012, 09:40 AM   #6
purbeast0
Lifer
 
purbeast0's Avatar
 
Join Date: Sep 2001
Posts: 37,640
Default

firebug ... download it.

or press F12 in chrome.
purbeast0 is online now   Reply With Quote
Old 11-20-2012, 12:05 PM   #7
clamum
Lifer
 
clamum's Avatar
 
Join Date: Feb 2003
Posts: 21,755
Default

Quote:
Originally Posted by sygyzy View Post
Try document.write(boardArray[i]);
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.
__________________
\ Battlefield 4 Stats /\ Heat /
clamum is offline   Reply With Quote
Old 11-20-2012, 02:10 PM   #8
ibex333
Diamond Member
 
ibex333's Avatar
 
Join Date: Mar 2005
Location: Ukraine, Odessa - New York City, Brooklyn ; )
Posts: 3,263
Default

Quote:
Originally Posted by purbeast0 View Post
firebug ... download it.

or press F12 in chrome.
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.



Quote:
Originally Posted by clamum View Post
Quote:
Originally Posted by sygyzy View Post
Try document.write(boardArray[i]);
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.
Thanks. That did it.
__________________
2500k Sandy Bridge @ 4.40GHz(Yay!)~GIGABYTE Z68A-D3H-B3~G.SKILL 8GB DDR3 1333~
3.14 Tb HD Space~2x PNY GTX 760 GTX SLi~HT Omega Claro~Cooler Master CM690~600w OCZ StealthXtream~ASUS 27" Monitor
HEATWARE
EBAY
ibex333 is offline   Reply With Quote
Old 11-20-2012, 03:58 PM   #9
KLin
Lifer
 
KLin's Avatar
 
Join Date: Feb 2000
Location: Phreaznaux
Posts: 28,770
Default

Quote:
Originally Posted by ibex333 View Post
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.
That's why you download it, use it, use google to research errors you don't understand, etc. etc. Practice, practice, practice.
__________________
'L_'
KLin is online now   Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:38 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.