michelle9090
michelle9090

Reputation: 287

Javascript function not linking to the next one

I'm building a chatbot, some of the scripts are as follows

var convpatterns = new Array (
new Array (".*hi.*", "Hello there! ","Greetings!"),
new Array (".*ask me*.", Smoking),
new Array (".*no*.", "Why not?"),

As you can see if user typed in "hi", the chatbot reply with either Hello there or Greetings! And if the user typed in "ask me a question", it links to the Smoking() function.

function Smoking(){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
return field
SmokingAnswer()

}

function SmokingAnswer(){
var userinput=document.getElementById("messages").value;
if (userinput="yes"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Oh no! Smoking is not good for your health!");
}else if(userinput="no"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Good to hear that you are not smoking!");
}
return field
}

So within the Smoking() function, the chatbot will verbally ask the user "Do you smoke?", and it should then link to the next function which is the SmokingAnswer() where the user can either type in yes or no and the chatbot will then give a reply based on the respond from the user. However right now if I typed in "ask me a question", the chatbot asks "Do you smoke?", but when I type in "no", instead of saying "Good to hear that you are not smoking!", the chatbot says "Why not?" based on the new Array.

Update (changed based on suggestion, but still not working):

function initialCap(field) {
field = field.substr(0, 1).toUpperCase() + field.substr(1);
return field
}
function Smoking(){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
SmokingAnswer()

}

function SmokingAnswer(){
var userinput=document.getElementById("messages").value;
if (userinput=="yes"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Oh no! Smoking is not good for your health!");
}else if(userinput=="no"){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Good to hear that you are not smoking!");
}
return field
}

Upvotes: 0

Views: 66

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074335

Equality comparisons in JavaScript use the == or === operator; = is always assignment. So here:

if (userinput="yes"){

...you're assigning "yes" to userinput and then going into the body of the if (because it ends up being if ("yes"), and "yes" is truthy, so we follow the branch). It should be:

if (userinput == "yes"){

or

if (userinput === "yes"){

The difference between == and === is that == (the "loose" equality operator) will do type coercion if necessary (sometimes in surprising ways) to try to make the operands equal, but === will not (operands of different types are always not equal).

Separately:

function Smoking(){
window.iSpeech.speak(speakPluginResultHandler,nativePluginErrorHandler,"Do you smoke?");
return field
SmokingAnswer()
}

The SmokingAnswer function will never be called there, because it's after the return field. So either the Smoking function returns before SmokingAnswer gets called, or it throws an error because field is undefined (it's not shown anywhere in your code); either way, SmokingAnswer is never called.


Side note: Your initial array can more concisely be written with array initializers:

var convpatterns = [
    [".*hi.*", "Hello there! ","Greetings!"],
    [".*ask me*.", Smoking],
    [".*no*.", "Why not?"],
    // ...
];

You might also look into regular expression literals rather than strings (/.*hi.*/ instead of ".*hi.*"), since you don't have to worry about two layers of escaping with a regex literal.

Upvotes: 2

Related Questions