home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 2918
Discussions: 188,379, Posts: 2,243,456, Members: 232,607
Old June 13th, 2008, 11:55 AM   Digg it!   #1 (permalink)
Junior Member
 
Join Date: Jun 2008
Posts: 4
Beginner in Javascript I need help!!

I have a class with a crap teacher I really need help with this as I cant get it to work. Here are the instructions.

The strings entered by the user are split at the space to give a two-element array, which is assigned to a new row in strs. Function check repeatedly prompts the user for a string and checks whether there is a pair in array strs that matches in the manner described above. It does this with a do/while loop. Index i starts at 0 and variable found is initialized to false. It goes through the rows in strs (indexed by i) until found becomes true or the rows are exhausted (i is equal to the length of strs).



Here is the code I have so far:
<html>
<head>
<script type=”text/javascript”>
var strs = new Array();
init( strs );
check( strs );
function init( strs )
{
while ( str = window.prompt(
"Enter a string with an initial and final segment"
+ " separated by a space",
"" ) )
strs=str.split(" ") ;
}

function check( strs )
{
while ( str = window.prompt( "Enter a string", "" ) )
{
var found = false,
i = 0;

do {
var loc1 = strs[i][0];
if ( loc1 != -1 ) {
var loc2 = strs[i][1]+ strs[i][0];
if ( loc2 != -1 )
found = true;
}
i++;
} while ( !found && strs.length>i );
if ( found )
alert("Found");
else
alert("Not found");
}
}
</script>
</head>
</html>

overdosed20 is offline   Reply With Quote
Old June 13th, 2008, 10:40 PM     #2 (permalink)
Super F@D Folder
 
Join Date: Jun 2004
Posts: 5,004
Send a message via AIM to sr71000
you should always put your code inside of code tags

[ code ]
your code here
[ /code ]

if you take out the spaces around the words code and /code you get

Code:
your code here
This will make it much easier for us to troubleshoot and help We don't like to give out answers with homework. If you want help, get started with the project and when you're stuck on something, as a more specific question. I know that I and others are glad to help but I know that at least I personally don't like to just give out answers.

-Kevin

sr71000 is offline   Reply With Quote
Old June 20th, 2008, 11:39 AM     #3 (permalink)
Junior Member
 
Join Date: Jun 2008
Posts: 4
I just can't figure out why nothing is displaying when I launch this from notepad.

Code:
<html>
<head>
<title>Quiz2</title>
<script type=”text/javascript”>
var strs = new Array();
init( strs );
check( strs );
function init( strs )
{
   while(str = window.prompt( 
                  "Enter a string with an initial and final segment"
                  + " separated by a space",
                  "" )) 
    
 strs=str.split(" ");
}
function check( strs )
{
  while ( str = window.prompt( "Enter a string", "" )  )  {
    var found = false,
        i = 0;
  
    do  {
      var loc1 = strs[i][0];
      if ( loc1 != -1 )  {
        var loc2 = strs[i][1]+ strs[i][0];

        if ( loc2 != -1 )
          found = true;
      }
      i++;
    } while ( !found && strs.length>i );
    if ( found )
      alert("Found");
    else
      alert("Not found");
  }
}
</script>
</head>
</html>

overdosed20 is offline   Reply With Quote
Old June 22nd, 2008, 10:01 PM     #4 (permalink)
Member
 
Join Date: Oct 2003
Posts: 255
change yoru javascript tag to the following:

your quotes are funky...once i changed them with the line below it ran through fine.


Code:
<script type="text/javascript">
amtrac24 is offline   Reply With Quote
Old June 22nd, 2008, 10:05 PM     #5 (permalink)
Member
 
Join Date: Oct 2003
Posts: 255
you need to fix your loops though.
amtrac24 is offline   Reply With Quote
Old June 22nd, 2008, 10:31 PM     #6 (permalink)
Member
 
Join Date: Oct 2003
Posts: 255
here you go buddy...

Code:
<html>
<head>
<title>Quiz2</title>
<script type='text/javascript'>
var strs = new Array();
strs=init( strs );
check( strs );

function init( strs )
{
var str='';

while (str=='')
{
   str = window.prompt( 
                  "Enter a string with an initial and final segment"
                  + " separated by a space",
                  "" )
}
  

 strs=str.split(" ");
 
 return strs;

}
function check( strs )
{
var str='';
var found = false;

while (str=='')
{
 	str = window.prompt( "Enter a string", "" ) 
}

	for (var i=0;i<strs.length;i++)
	{
		if (strs[i]==str)
		{
		   found=true;
		}
	}
	
	if (found)
		alert('Found');
	else
		alert('Not Found');
}
</script>
</head>
</html>
amtrac24 is offline   Reply With Quote
Old June 24th, 2008, 12:34 PM     #7 (permalink)
Junior Member
 
Join Date: Jun 2008
Posts: 4
Thanks Amtrack that worked great.
overdosed20 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner Javascript question James Engle Webmastering and Programming 0 April 21st, 2008 11:43 PM
Beginner, javascript code help! snikker74 Webmastering and Programming 4 April 4th, 2008 12:08 PM
Beginner needs help mewelch Motherboards 1 February 1st, 2007 10:29 PM
Beginner Guitar help............ Cyclone2 IMO Community 31 July 3rd, 2004 09:49 PM
im a beginner..... couch potato Webmastering and Programming 18 January 26th, 2003 09:44 PM

Most Active Discussions
Is It Just Me? (2883)
The United States Debt (20)
Looks like Burris will get his Sena.. (8)
I think I just killed my computer w.. (24)
Upgrading RAM (5)
Folderchat Weekday thread (439)
Antec 300 bulk purchase? (11)
Worth the upgrade?? (14)
Titan quest and Immortal Throne, an.. (17)
Recent Discussions
Help with an Ati Radeon HD 4850.. (24)
Install Problem for Windows Def.. (0)
New Build ( Finally ) (1)
dual monitors wont boot (0)
Folderchat Weekday thread (439)
MSN Hotmail Down??? (7)
Laptop waking up itself (0)
CPU wont boot (3)
Best digital camera for under 2.. (13)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 08:29 PM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28