JS - Login condition example

0 votes
198 views
added Jul 6, 2018 in Javascript by LC Marshal Captain (25,790 points)
edited Jul 12, 2018 by LC Marshal

If username as "admin", then prompt a password, if the input is empty, “Canceled.”, if it’s another string – then show “I don’t know you”.

The password is checked as follows:

  • If it equals “sacredpass”, then show “Welcome!”,
  • Another string – show “Wrong password”,
  • For an empty string or cancelled input, show “Canceled.”
let loginName = prompt('your login name', '');
  
if (loginName == 'admin') {
  let password = prompt('password','') 
    if (password == 'sacredpass') {
      alert('Welcome');
    } else if (password == '' || password == null) {
      alert('canceled');
    } else {
      alert('Wrong password')
    }
} else if (loginName == '') {
  alert('canceled')
}
else {
  alert('Dont know you')
}
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...