1

My friend is working on a fun Javascript web application where you type something in the text box, and the computer returns you a result, but it is case-sensitive. Can he make it case-insensitive? We have tried to use:

var areEqual = string1.toUpperCase() === string2.toUpperCase();

which was on JavaScript case insensitive string comparison, but he cannot figure out how to use that.

function isValid() {
    var password = document.getElementById('password').value;

    if (password == "Shut Up")
        { alert('HOW ABOUT YOU!') }

    else if (password == "No")
        { alert('You just did') }

}
hello
  • 19
  • 4
  • 1
    This post is not suitable for redaction as there is no PII involved (phone/email/IP addresses). You can request that this question be disassociated from your account instead. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request](//meta.stackoverflow.com/q/323395/584192)? – Samuel Liew Jan 07 '19 at 07:50

3 Answers3

1

Please, if you're going to do this, use switch! The only real difference between toLowerCase as opposed to toUpperCase here is that the values in the case lines won't be shouting at you.

function isValid() {
    var password = document.getElementById('password').value;

    switch (password.toLowerCase()){
        case "shut up":
            alert('HOW ABOUT YOU!');
        break;
        case "no":
            alert('You just did');
        break;
        case "okay":
            alert('Okay');
        break;

        // ...

        case "something else":
            alert('Really?');
        break;
        default:
            alert('Type Something Else')
    }
}
SeinopSys
  • 8,787
  • 10
  • 62
  • 110
0

Just add the .toUpperCase() behind your variables:

else if (password == "No")
  {alert('You just did')}

becomes:

else if (password.toUpperCase() == "No".toUpperCase())
  {alert('You just did')}

or just:

else if (password.toUpperCase() == "NO") //Notice that "NO" is upper case.
  {alert('You just did')}
mihyar
  • 29
  • 7
0

You can use that this way, for example in the first else-if block:

 else if (password.toUpperCase() == "No".toUpperCase())
 {alert('You just did')}

The function toUpperCase, applied to a string, returns it's uppercased version, so for No it would be NO. If the password variable holds any lower-upper case combo of the word no, such as "No", "nO", "NO" or "no", then password.toUpperCase() will be "NO". The previous code is equivalent to

 else if (password.toUpperCase() == "NO")
 {alert('You just did')}
Nillus
  • 1,131
  • 1
  • 14
  • 32