June 13th, 2008, 11:55 AM
|
#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> |
| |
June 13th, 2008, 10:40 PM
|
#2 (permalink)
| | Super F@D Folder
Join Date: Jun 2004
Posts: 5,004
|
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 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 |
| |
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> |
| |
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"> |
| |
June 22nd, 2008, 10:05 PM
|
#5 (permalink)
| | Member
Join Date: Oct 2003
Posts: 255
|
you need to fix your loops though. |
| |
June 22nd, 2008, 10:31 PM
|
#6 (permalink)
| | Member
Join Date: Oct 2003
Posts: 255
| 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> |
| |
June 24th, 2008, 12:34 PM
|
#7 (permalink)
| | Junior Member
Join Date: Jun 2008
Posts: 4
|
Thanks Amtrack that worked great. |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |
Posting Rules
| You may post new threads You may post replies You may not post attachments You may not edit your posts HTML code is Off | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |