-1

How can I access any item from a list in a function? I can only use a specific list item in a function and I can't figure out how to use any list item I want. If I want to use the second item in a list, I have no idea how to do it.

var database = [
    {
        username: "captain",
        password: "america"
    },
    {
        username: "bruce",
        password: "banner"
    },
    {
        username: "tony",
        password: "stark"
    }
]

var usernamePrompt = prompt("Enter your name: ");
var passwordPrompt = prompt("Enter your password: ");

function signIn(user, pass) {
    if (user === database[0].username && pass === database[0].password) {
        alert("Welcome" + database[0].username)
    }
    else {
        alert("Wrong username or password!")
    }
}

signIn(namePrompt, realNamePrompt)
  • As a side note if this is a literal login process for a website this is extremely unsafe – Libra Nov 10 '21 at 17:34
  • You can use `for` loop to iterate through an array, but better solution is to use `forEach` method: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – balrundev Nov 10 '21 at 17:34
  • Use a for-loop or change your data structure to {captain: america, tony: stark} and compare using something like `database[user] === pass`. – enzo Nov 10 '21 at 17:36
  • 3
    @Laif This is not used for any website. I am just trying to learn functions and lists. :) – greasemach1ne Nov 10 '21 at 17:37

2 Answers2

1

Mmmm to access any, you would need to pass the index you want to access like this

function check(user, pass, index) {
  if (user === database[index].username && pass === database[index].password) {
        return user;
   } return null;
}

then you could maybe do a loop, outside of this function to cover all the indexes, something like this:

for (int i = 0; i < USERS_LENGTH; i++) { const user = check (user, pass, i); if (user) return user; }

or a more functional approach with Array.find , something like this:

database.find(u => check(u))

ALTHOUGH, I don't think this is the correct approach to handle real database logins, this is brute force, and in a million records data base, this will take forever.

Jumper
  • 151
  • 1
  • 14
  • The issue comes from the fact that he is lacking a comma in his list declaration, therefore only the first item is valid. – Libra Nov 10 '21 at 17:38
1

You can use find method on javascript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find).

    var database = [
        {
            username: "captain",
            password: "america"
        },
        {
            username: "bruce",
            password: "banner"
        },
        {
            username: "tony",
            password: "stark"
        }
    ];
    
function signIn(user, pass) {
    const user= database.find((data) => data.username === user && data.password === pass);
    if (user) {
        alert("Welcome" + user.username)
    }
    else {
        alert("Wrong username or password!")
    }
}