jQuery / Ajax simple chatbot, how ignore lowercases [duplicate] - javascript

This question already has answers here:
How to do case insensitive string comparison?
(23 answers)
Closed 7 years ago.
I am currently programming a simple chatbot but I getting trouble with the input identifier.
if (message.indexOf("how are you")>=0){
send_message("Thanks, Iam good!");
responsiveVoice.speak("Thanks, Iam good!");
Is person is tying in "How are you" the answer will not be triggered since its not lower case. I could copy/paste if state with "How" but is there a easier way? Please be gentle, aim new to programming. Thank you!

Use .toLowerCase()
if (message.toLowerCase().indexOf("how are you") >= 0)

You could try toLowerCase function of javascript.
messaje.toLowerCase().indexOf("how are you")...
Function:
http://www.w3schools.com/jsref/jsref_tolowercase.asp

Related

Is there in javascript to use includes in an if statement or switch? [duplicate]

This question already has answers here:
How can I use ranges in a switch case statement using JavaScript?
(7 answers)
Closed 2 years ago.
I'm building an app to check the expiration dates of items. The functionality of the app works but I want to be able to check if a string includes a sub-string in order to run my function which is a countdown timer. I haven't included much of the code but it should be sufficient. Code is Bellow
var userInput = prompt("What item would you like to look up?");
switch (userInput) {
case userInput.includes("cous cous"):
//run a setInterval function
case expression: resolves the expression and does an equality test of that value against the userInput.
Use an if() statement for this type of thing.

Please tell me what this code means [duplicate]

This question already has answers here:
What is the result of this javascript?
(3 answers)
Closed 5 years ago.
can you tell me what this code does as I need to understand it for a coding assessment. I only just started learning JavaScript
function getAttackString() {
var foo = "d323b8b34";
var bar = "x334q3j98";
return "The code is: "+(foo.substr(3,foo.length-6))+(bar.substr(2));
}
Thanks for your help
Start by looking up what .length and .substr are.
For example, what will foo.length give you? A good way to find out would be:
var x = "abc123";
console.log(x.length);
console.log is a command to print information to your web browser console, very helpful when doing debugging work.

How do we convert jQuery $('div#item').html() in VanillaJS [duplicate]

This question already has answers here:
How can I use innerhtml in JavaScript?
(4 answers)
Closed 5 years ago.
I need to convert jQuery method to VanillaJS
$('div#item').html()
can convert .html() using innerHTML. But have the issue with $('div#item'). I used document.getElementById('div#item').innerHTML It's not working for me. Someone please suggest any better solution.
just use the actual id of the element you want to acess
document.getElementById('item').innerHTML
Try
document.getElementById('item')
You may use vanilla JS for this task
document.querySelectorAll('div#item')[0].innerHTML;
try this:
console.log (document.querySelector('div#item').innerHTML)
<div id="item">ololo</div>

searching values in array javascript [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.

Is there a JavaScript alternative to isNaN()? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript logic for isNaN()
Hey all I want is to check that the entered value in a textbox is not a number or without using the function isNaN();. Is there any way?
parseFloat(value)!== +value;
But what is wrong with isNaN(value)?

Categories

Resources