PDA

View Full Version : Need help with school for if loop problem!!


rugerky
01-31-2010, 02:03 PM
Hi all
I'm a student and need help with this javascrip program. I can't get it to work right you can put user1 and paas2 and it still lets you in. I don't know how loops works. Thanks

<html>

<head>
<title>assign 2 JavaScript</title>
</head>

<body>
<h1>Logon to your JavaScript Class</h1>
<script language="JavaScript" type="text/javascript">
// Assignment 2 CSWB 120 Spring 2010 //

{
var userid = new Array();
userid[0] = "user1";
userid[1] = "user2";
userid[2] = "user3";

var password = new Array();
password[0] = "pass1";
password[1] = "pass2";
password[2] = "pass3";


var user_answer = prompt ("What is your user id?","");
user_answer = user_answer.toLowerCase();


for(array_counter = 0; array_counter <3; array_counter++)

{
if (user_answer == userid[array_counter])
{


var user_answer = prompt ("What is your Password?","");
user_answer = user_answer.toLowerCase();
}

else

{
}
{
document.write("<p> <font color=red> ERROR:Userid not vaild Please try again. </font></p>");
}

}
for(array_counter = 0; array_counter <3; array_counter++)


{
if (user_answer == password[array_counter])

{

document.write("<p> <font color=blue>Welcome to the JavaScript Class. </font></p>");

}

else

{

document.write("<p> <font color=red> ERROR: Password not vaild Please try again. </font></p>");

}


}






}
</script>




</body>

</html>

Thanks for any help you can give me.

Spudhead
02-03-2010, 11:33 AM
Hi. Couple of things first of all:

1. The posting rules (http://www.codingforums.com/rules.htm) ask that you don't post homework assignments. We're here to help but we can't give you the answers.

2. When you're posting code, please wrap it in [CODE] tags to make it easier for us to read.

Regarding your question, this reference (http://www.w3schools.com/JS/js_loop_for.asp) might help your understanding of loops, although you don't seem to far off with that part.

Two things to consider:

1. userid.length will return the number of items in the array. It might be better to use that in your loop, so if you change the number of items in the array, you don't have to remember to change the number in your loop.

2. You're reusing the variable user_answer for both the userid input and their password input. This is causing you problems - using a different variable name for each piece of information will make it easier for you to track what's going on in your script.

rugerky
02-03-2010, 01:39 PM
Hi. Couple of things first of all:

1. The posting rules (http://www.codingforums.com/rules.htm) ask that you don't post homework assignments. We're here to help but we can't give you the answers.

2. When you're posting code, please wrap it in [CODE] tags to make it easier for us to read.

Regarding your question, this reference (http://www.w3schools.com/JS/js_loop_for.asp) might help your understanding of loops, although you don't seem to far off with that part.

Two things to consider:

1. userid.length will return the number of items in the array. It might be better to use that in your loop, so if you change the number of items in the array, you don't have to remember to change the number in your loop.

2. You're reusing the variable user_answer for both the userid input and their password input. This is causing you problems - using a different variable name for each piece of information will make it easier for you to track what's going on in your script.

Ok Thanks.
I'm just so confused about this. I can't seems to understand how to get it to work right.... It all ways loops though all three. My teacher is upset with me because I can't get it too. He just says its right there in front of me but I can't see it. If I can't get this I might as well drop out of class.

Thanks again for your help and your answer.

Spudhead
02-03-2010, 04:30 PM
My best advice would be to write out, in comments, the logic of your script. Then you can almost fill it in with the code that does whatever the comments describe.

So you might start with:

/*
Create array of login details
*/



/*
Ask the user for their username
*/



/*
Loop through the login details array, looking for the username they entered.
If I find it, make a note of it.
If I don't, alert the user.
*/



/*
If I've find a matching username, note the password that corresponds to it, and ask the user for their password
*/



/*
Check that the password they entered matches the one I have made a note of.
*/



/*
If the passwords don't match, alert the user.
*/

rugerky
02-04-2010, 02:49 PM
My best advice would be to write out, in comments, the logic of your script. Then you can almost fill it in with the code that does whatever the comments describe.

So you might start with:

/*
Create array of login details
*/



/*
Ask the user for their username
*/



/*
Loop through the login details array, looking for the username they entered.
If I find it, make a note of it.
If I don't, alert the user.
*/



/*
If I've find a matching username, note the password that corresponds to it, and ask the user for their password
*/



/*
Check that the password they entered matches the one I have made a note of.
*/



/*
If the passwords don't match, alert the user.
*/

:confused: Like I said I'm really confused about this!!! Why does it keep looping when it find the right id and password? I run it and it said found on one of them then comes up with the error on the other two, What I can't get is how to stop it. ok Thanks but I guess I will just have to drop the class because this has really got me stressed too. Thanks to everyone that tryed to help me.
Ruger

Spudhead
02-05-2010, 09:47 AM
Ok. Look. Here's what happens when we step through your code, line by line:

var user_answer = prompt ("What is your user id?","");
user_answer = user_answer.toLowerCase();

So we've now got their user ID, stored in a variable called "user_answer".

Fine, but we know that we're going to ask the user for at least two answers, so storing one of them in a variable called "user_answer" probably isn't great. Storing their user ID in a variable called... oh, lets say: "user_id", will lessen the potential for confusion.

for(array_counter = 0; array_counter <3; array_counter++){

Ok, so we've started looping. We're going to loop three times because we've written it right there in the loop. But since what we actually want to do is loop through each element of an array, we may as well use array.length (http://www.plus2net.com/javascript_tutorial/array-length.php) to get the actual number of elements and use that to control how many times we're looping.

if (user_answer == userid[array_counter]){

This makes sense: see if the current element in the userid array matches the value that we just asked the user for; the one that we stored in the variable user_answer. And if it does, do this:

var user_answer = prompt ("What is your Password?","");
user_answer = user_answer.toLowerCase();

Logically, this makes sense: we've found a matching user ID, so ask them for their password.

But wait: here's that user_answer variable again. And this time it's being used for the password! In the middle of a loop that compares the value of a variable called user_answer, we've just changed the value of that variable. That's going to cause us problems.

Let's move on for now though:

else

{
}
{
document.write("<p> <font color=red> ERROR:Userid not vaild Please try again. </font></p>");
}

I can sort of see what you're trying to do here, but the syntax is... a little out.


You're saying "if we don't find the user ID that they entered, in our array of user IDs, write an error". Couple of problems, though:

First, you're doing this while you're still inside the loop. So even if it does find a value in the array that matches what they entered, it'll do the error-writing bit for every value it finds that doesn't match.

Second, you've got way to many brackets up there. You can delete the first two, leaving just the ones around the document.write() bit. This won't solve the first issue, but it'll help.

Next line:

}
for(array_counter = 0; array_counter <3; array_counter++)

Ok, so the bracket closes your first loop. And then we start another loop.


if (user_answer == password[array_counter])

{

document.write("<p> <font color=blue>Welcome to the JavaScript Class. </font></p>");

}

else

{

document.write("<p> <font color=red> ERROR: Password not vaild Please try again. </font></p>");

}

Fine. Logical, syntax all makes sense. It's just in completely the wrong place in your code. Again: writing out what you need your script to do, in simple, logical steps - before you write a single line of actual code - will help you immensely:


Get their username
Get their password
Find their username in the array
If we find it, check that the corresponding password matches the password they entered


You can then go through each of those steps, breaking each down into its own set of steps.

Have another go at your script. Don't worry if it doesn't make sense; it will do eventually. I can't do it for you, but I can help you understand as best I can. Keep asking questions.

rugerky
02-09-2010, 03:04 PM
Thanks for your help on this. I have tried all weekend but I just don't seem to get it.
I dropped out of class because if i don't get this simple problem its only going to get harder. I'm going to keep on trying anyway. If you want to keep teaching me I would be thankful.
Thanks again
Roger

Spudhead
02-10-2010, 03:33 PM
Well, I'd try building your script again, using those four steps at the end of my last post as a guide.

You've got all the code to do it. All you need to do it put it in the right order.

rugerky
02-13-2010, 07:58 PM
Here it don't lop now I guess. What did I do wrong now.

<html>

<head>
<title>assign 2 JavaScript</title>
</head>

<body>
<h1>Logon to your JavaScript Class</h1>
<script language="JavaScript" type="text/javascript">

var userid = new Array();
userid[0] = "user1";
userid[1] = "user2";
userid[2] = "user3";

var password = new Array();
password[0] = "pass1";
password[1] = "pass2";
password[2] = "pass3";


var user_id = prompt ("What is your user id?","");
user_id = user_id.toLowerCase();

for (var count = 0; count < user_id.lenght; count++);


{
if(user_id == userid[count])

{

var user_password = prompt ("What is your Password?","");
user_password = user_password.toLowerCase();



}
else
{
document.write("<p> <font color=red> ERROR:Userid not vaild Please try again. </font></p>");
}
}

for (var count = 0; count < password.lenght; count++);

{
if (user_password == password[count])

{
document.write("<p> <font color=blue>Welcome to the JavaScript Class. </font></p>");

}
else
{

document.write("<p> <font color=red> ERROR: Password not vaild Please try again. </font></p>");

}

}

</script>


</body>

</html>