1
database = [
  {
    username: 'mutho',
    password: 'muth',
  },
  {
    username: 'pica',
    password: '1234',
  },
  {
    username: 'rudy',
    password: '1235'
  }
];
news = [
  {
    username: 'sarah',
    timeline: 'Hellow',
  },
  {
    username: 'ingrid',
    timeline: 'hello world'
  },
  {
    username: 'rudy',
    timeline: 'secret',
  }
];

function isvalid(user, pass) {
  database.forEach(function (item, index) {
    if (item.username === user && item.password === pass) {
      return true;
    }
    return false;
  });
}

function signIn(user, pass) {
  if (isvalid(user, pass) === true) {
    console.log(news);
  }
  else {
    alert("Your Username and password wrong");
  }
}

var userprompt = prompt("Input your username : ");
var passprompt = prompt("Input your password : ");

signIn(userprompt, passprompt)

I have some problem, I want to show news is the user and password are right in the database. but when I run this program, it always showing "Your Username and password wrong". what should I do?

thank you for the helping

Renumb
  • 23
  • 3

4 Answers4

0

You need Array#some and return the result of this method.

function isvalid(user, pass) {
    return database.some(function(item, index) {
        return item.username === user && item.password === pass;
    });
}

function signIn(user, pass) {
    if (isvalid(user, pass)) {
        console.log(news);
    } else {
        alert("Your Username and password wrong");
    }
}


var database = [{ username: 'mutho', password: 'muth' }, { username: 'pica', password: '1234' }, { username: 'rudy', password: '1235' }],
    news = [{ username: 'sarah', timeline: 'Hellow' }, { username: 'ingrid', timeline: 'hello world' }, { username: 'rudy', timeline: 'secret' }],
    userprompt = prompt("Input your username:"),
    passprompt = prompt("Input your password:");

signIn(userprompt, passprompt);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Maybe you re looking for something like this:

function findValidUsers(user, pass) {
   return database.filter(item => item.username === user && item.password === pass)
}

where findValidUsers will return a array of all users in your database array that have the same username and password.

e.g: findValidUser('mutho','muth')

would return {username: 'mutho', password: 'muth'}

Michel Vorwieger
  • 692
  • 5
  • 14
0

foreach doesn't return any value (or break and early return).

Use a standard for loop. Example https://stackoverflow.com/a/35450142/3327009

Also, the way you have done it now it will just check your first object in the array (if you translate to a for loop)

You need something like this

var i
for (i=0; i<database.length;i++)
    if(foobar)
         return true
return false
Niclas Lindgren
  • 542
  • 6
  • 23
0

Your return true does NOT break your loop, it just ends your callback.
See this: Short circuit Array.forEach like calling break. Or that: https://stackoverflow.com/a/6260865/3872061. It might help you understand where your problem lies.

fpierrat
  • 739
  • 7
  • 25