-2

i have a chatbot that answers questions only if i write them the same way as in the code Ex: If i wrote the tag "Hello" it will not answer if i say in the chatbot "hello" . I have to write it with a capital letter like in the code. Is there a function that will ignore that and answer it even if i write it "HeLlO"?

if (message.indexOf("Bye")>=0 || message.indexOf("bye")>=0 ||  message.indexOf("Goodbye")>=0  ||  message.indexOf("Goodbye")>=0  ){
    send_message("You're welcome.");
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375

4 Answers4

3

You could use a regular expression in case-insensitive mode:

if (/bye/i.test(message)) {
    send_message("You're welcome.");
}

Also, there's no need to test for both bye and goodbye -- if it contains goodbye then it obviously also contains bye.

But if you want to test for different messages, the regexp also makes this easy:

if (/bye|adios|arrivederci/i.test(message))
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Try message.toLowerCase() to convert uppercase letters to lowercase.

Ben Aubin
  • 5,542
  • 2
  • 34
  • 54
0

You can try using the toLowerCase method to do the trick.

Example:

msg = "HeLlo"
msg.toLowerCase() // "hello"
msg.toLowerCase().indexOf("hello")>=0 // true

Or you can make use of the String prototype to create a case-insensitive contains method:

String.prototype.ci_contains = function(str){
    return this.toLowerCase().contains(str.toLowerCase())
}

Use Example:

msg = "ByE"
msg.ci_contains("bye") // True
msg.ci_contains("Bye") // True
Richard Cotrina
  • 2,525
  • 1
  • 23
  • 23
0

I sugest to use the:

  • .toLowerCase()

This helps to compair without case sensitive i.e:

  function compairWithNoCase(valueCompair1,valueCompair2)
    {
        return valueCompair1.toLowerCase().match(
                                         valueCompair2.toLowerCase()
                                                )==valueCompair1.toLowerCase()
    }
/*
compairWithNoCase('hElLo','HeLlO');
true
compairWithNoCase('BYE','HELLO');
false
compairWithNoCase('ByE','bYe');
true
*/
Ricardo Figueiredo
  • 1,416
  • 13
  • 10