0

I'm trying to code a password variable that can hold multiple values so any of the following passwords can work and give access to the site.

var pass1="testpass1", "testpass2";

password=prompt('Whats the pass?',' ');

if (password==pass1)
  alert('ja');
else
   {
    alert('nein')
    window.location="https://google.com/";
}

I really need to figure this out.

Thank you!

zeero
  • 3
  • 1
  • Does this answer your question? [What's the prettiest way to compare one value against multiple values?](https://stackoverflow.com/questions/9121395/whats-the-prettiest-way-to-compare-one-value-against-multiple-values) – lusc Mar 09 '22 at 16:06
  • Use a [SWITCH STATEMENT](https://www.w3schools.com/js/js_switch.asp) – Zak Mar 09 '22 at 16:07
  • 4
    Just... you know that anyone with opened console can get correct password from code and then bypass your security? Consider using Apache .htpasswd instead – Justinas Mar 09 '22 at 16:08
  • 4
    -- And please note, this is fine and dandy for playing with and testing javaScript .. But be aware your source code is viewable by anyone on the internet. Do not put secure items or information behind these passwords. – Zak Mar 09 '22 at 16:09
  • 1
    Your nearly correct with your code, an array is what your likely looking for -> `var pass1=["testpass1", "testpass2"]; if (pass1.includes('testpass1')) console.log('your the man');` – Keith Mar 09 '22 at 16:09
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Lennert Mar 09 '22 at 16:37

1 Answers1

2

You could make pass1 an array and then use the .includes method like so:

let pass1=["testpass1", "testpass2"];

password=prompt('Whats the pass?');

if (pass1.includes(password)){
  alert('ja');
}
else{
    alert('nein')
    window.location="https://google.com/";
}
Lennert
  • 96
  • 1
  • 7