R
R
Ruslan2020-11-26 19:02:05
JavaScript
Ruslan, 2020-11-26 19:02:05

Why does not see the second array object?

Hello. There is a task:

Write a simple authorization program. Create an array users, in which each element is an object of type "user" with the properties "name", "login" and "password". The program should prompt the user for a login and password using prompt, and then go through the users array and look for an object with the same values ​​in it. If the user is found in the users array, then the program should greet him by name. Otherwise, an authorization error should appear.

Did it like this:
spoiler
let authorization = [
    {
        username: 'Ruslan',
        login: 'Lorelin',
        password: '12345'
    },

    {
        username: 'Margarita',
        login: 'ririka',
        password: '54321'
    }
];

let login = prompt('Введите логин');
let password = prompt('Введите пароль');

let user = null;

for (let i = 0; authorization.length; i++) {
    if (authorization[i].login == login && authorization[i].password == password) {
        user = authorization[i];
        alert('Привет, ' + user.username);
        break;
    }
    else {
        alert('Логин или пароль не верен.');
        break;
    }
}


The problem is that the program displays that the password or login is not correct if you enter the data of the second array object, if the first one, then everything is ok. If you remove else, then everything works fine, what could be the reason?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question