Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 612303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:52:42+00:00 2026-05-13T17:52:42+00:00

This is for a Java script course that Im taking… I need to create

  • 0

This is for a Java script course that Im taking…

I need to create a simple user-login script. When the page loads a prompt pops up asking for the user name. If the correct username is entered, another prompt is displayed asking for a password.

If both the username and valid, then you a sucessful message, otherwise you get a failure message.

I’ve written all of the code, but am having trouble with validating the username and password.
You can see in my code that I’m using two array lists. One for users and another for passwords. When I run my code, if I type in the correct username and password for user1, it validates, BUT if I enter user1 and the password for user2, it still validates.

<script type="text/javascript">
//Input from user
    var userId = prompt("Enter UserID", "");
        userId_lowercase = userId.toLowerCase();


//Username and Password Arrays
    var userIdList = new Array();
        userIdList[0] = "user1";
        userIdList[1] = "user2";
        userIdList[2] = "user3";

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

//Process input and check for authentication

    //Check for correct userId

        var userIdFound;

        for (index in userIdList) 
        {
            if (userId_lowercase == userIdList[index]) 
            {
                userIdFound = "Y";
                break;
            }
        }

        if (userIdFound == "Y") 
        {
            document.write("<p>" + userId + " was Found</p>"); ;
            //Check for correct Password
            var password = prompt("Enter Password", "");
            var passwordFound;

            for (index in passwordList) 
            {
                if (password == passwordList[index]) //  This is where I need help, 
                                                     //  how do I say 
                                                     //  "if password is from the passwordList && it matches the userId index selected"

                {
                    passwordFound = "Y";
                    break;
                }
            }

            if (passwordFound == "Y") 
            {
                document.write("<p>Welcome to the class!</p>"); 
            }
            else 
            {
                document.write("<p>Error: Password is not valid.</p>"); 
            }//END Password Check

        } 
        else 

        {
            document.write("<p>" + userId + " was NOT found</p>"); 
        }//END USERID Check

</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:52:43+00:00Added an answer on May 13, 2026 at 5:52 pm

    First of all, I wouldn’t use for-in, I’d use a simple for loop to iterate over the array. Then you could store the index that matches the user name to compare it to the password array:

    var index;
    var userIndex;
    for (index = 0; index < userIdList.length; index++) {
                if (userId_lowercase == userIdList[index]) 
                {
                    userIndex = index;
                    userIdFound = "Y";
                    break;
                }
    }
    

    Then, in your password loop you’d say:

    if (password == passwordList[index] && index == userIndex)
    {
        passwordFound = "Y";
        break;
    }
    

    That’s the simple way around the problem. I don’t know how much you’ve learned but you could also use an array of objects:

    var userLogins = [{user:"user1", password:"pass1"},...etc];
    

    And then loop through that array once after asking for the name and password and seeing if they match:

    for (index = 0; index < userLogins.length; index++) {
        if (userLogins.user == userId_lowercase && userLogins.password == password) {
            //Do stuff here
        }
    }
    

    The thing to keep in mind is that you have to link the user name to the password somehow. The way you’re currently doing this is by the index in both arrays, but that isn’t guaranteed. It’d be much better to link the two bits of information in some way, as in the array of objects above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Since you are passing them by value, you are coping… May 15, 2026 at 4:15 pm
  • Editorial Team
    Editorial Team added an answer Try HitCount and/or MissCount API. Something like this..... int oldMissCount… May 15, 2026 at 4:15 pm
  • Editorial Team
    Editorial Team added an answer You better style with css, something like : links.append($("<a class='link'… May 15, 2026 at 4:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.