JQUERY, AJAX if else statement

JQUERY, AJAX if else statement

本文关键字:else statement if AJAX JQUERY      更新时间:2023-09-26

我尝试制作一个简单的聊天机器人。到目前为止,机器人的答案是定义问题中的用户类型。但是,如果用户问了一些未定义的问题,我怎么能让聊天机器人发出"对不起,我不明白"?

function ai(message){
    if (username.length<3){
        username = message;
        send_message("Nice to meet you " + username + ", how are you today?");
        responsiveVoice.speak("Nice to meet you " + username + ", how are you today?");
    } 
    if (message.toLowerCase().indexOf("how are you")>=0){
        send_message("Thanks, Iam good!");
        responsiveVoice.speak("Thanks, Iam good!");
    }
    if (message.toLowerCase().indexOf("time")>=0){
        var date = new Date();
        var h = date.getHours();
        var m = date.getMinutes();
        send_message("Current time is "+h+":"+m);
    }
    if (message.toLowerCase().indexOf("thanks")>=0){
        send_message("You are welcome");
    }
    if (message.toLowerCase().indexOf("Thank you")>=0){
        send_message("No Problem");
    }
    if (message.toLowerCase().indexOf("thank you very much")>=0){
        send_message("Welcome Sir!");          
    } 
}

使用else

if (message.toLowerCase().indexOf("how are you")>=0){
  send_message("Thanks, Iam good!");
  responsiveVoice.speak("Thanks, Iam good!");
} else {
  send_message("sorry I don't understand");
  responsiveVoice.speak("sorry I don't understand");
}

您也可以使用switch语句:

switch (message.toLowerCase()) {
  case "how are you":
    send_message("Thanks, Iam good!");
    responsiveVoice.speak("Thanks, Iam good!");
    break;
  case "something else":
    send_message("something else");
    responsiveVoice.speak("something else");
    break;
  default:
    send_message("sorry I don't understand");
    responsiveVoice.speak("sorry I don't understand");
}

Teemu 提供的答案

 function ai(message){
            if (username.length<3){
                username = message;
                send_message("Nice to meet you " + username + ", how are you today?");
                responsiveVoice.speak("Nice to meet you " + username + ", how are you today?");
                return;
            } 
            if (message.toLowerCase().indexOf("how are you")>=0){
                send_message("Thanks, Iam good!");
                responsiveVoice.speak("Thanks, Iam good!");
                return;
            }
            if (message.toLowerCase().indexOf("time")>=0){
                var date = new Date();
                var h = date.getHours();
                var m = date.getMinutes();
                send_message("Current time is "+h+":"+m);
                return;
            }
             if (message.toLowerCase().indexOf("thanks")>=0){
                send_message("You are welcome");
                 return;
            }
             if (message.toLowerCase().indexOf("Thank you")>=0){
                send_message("No Problem");
                 return;
            }
             if (message.toLowerCase().indexOf("thank you very much")>=0){
                send_message("Welcome Sir!"); 
            } else {
  send_message("sorry I don't understand");
  responsiveVoice.speak("sorry I don't understand");
}