Ok. Look. Here's what happens when we step through your code, line by line:
Code:
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.
Code:
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 to get the actual number of elements and use
that to control how many times we're looping.
Code:
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:
Code:
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:
Code:
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:
Code:
}
for(array_counter = 0; array_counter <3; array_counter++)
Ok, so the bracket closes your first loop. And then we start another loop.
Code:
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.