Javascript/Node AI - How to use match to repeat a word from an array of words

Problem

Really need some help with this one. Iv'e done an AI with javascript and Node.js. I have harcoded a bunch of Questions and answers, iv'e also made it possible for the user to change the IA's answer if he wants to.

Most javascript code ha been made on the server-side.

I need to make it possible for the AI to repeat a specific word. For instance:

If the user writes -"Where do you live?", the ai will return "In middle earth my precious" because of the aray list:

var Question4 = { Question: "Where do you live?", Response: "In Middle Earth my precious" };

I have to make it so that the user writes "live" it will repeat "live". I was thinking of using the match variabel but i'm not sure. Som input would be appreciated!

BestAnswer returns the new answer if the user has changed it, otherwise it will return the default answer. Here is the array and the code that is being sent to the client/html-side:

var Question1 = { Question: "Hello", Response: "Hello precious" };
var Question2 = { Question: "What is your name?", Response: "My name is " + aHobbit.name + " " + "precious" };
var Question3 = { Question: "How old are you?", Response: "I'm " + aHobbit.age + " " + "my love" };
var Question4 = { Question: "Where do you live?", Response: "In Middle Earth my precious" };
var Question5 = { Question: "What do you like?", Response: "We love the precious. Yummy food we like, raw fish, rabbits, all of them.<br> I like them raw and raddeling. Yes precious raw we like them" };
var Question6 = { Question: "What don't you like?", Response: "Filthy orcsisses, stupid fat hobbitsses.<br> Yes precious..  but juicy and tender they are.." };
var Question7 = { Question: "How are you?", Response: "We are so happy precious oh yees..<br> Up and down down and up.. up up up we go.. Smeagoooool!!" };


var AllQueries = [Question1, Question2, Question3, Question4, Question5, Question6];


app.post("/creature", function (req, res) {

    var aQuestion = req.param("question");
    //var BestQuestion = req.param("bestQuestion");
    var BestAnswer = req.param("bestAnswer");

    var length = AllQueries.length;

    Answer = "What does it ask us?? Gollum! Gollum!!...";

   for (var i = 0; i < length; i++) {
        if (AllQueries[i].Question === aQuestion) {
            if (BestAnswer != undefined && BestAnswer.trim().length > 0) {
                AllQueries[i].Response = BestAnswer;
            }
            Answer = AllQueries[i].Response;
        }

   }



    res.sendfile("public/index.html");

And here is a screen on what it looks like:

http://postimg.org/image/nbf3w6wr7/full/

Regards

Chris

Problem courtesy of: koffe14

Solution

If I understand the question correctly this may work:

var Question1 = { Question: "Hello", Response: "Hello precious" };
var Question2 = { Question: "What is your name?", Response: "My name is " + aHobbit.name + " " + "precious" };
var Question3 = { Question: "How old are you?", Response: "I'm " + aHobbit.age + " " + "my love" };
var Question4 = { Question: "Where do you live?", Response: "In Middle Earth my precious" };
var Question5 = { Question: "What do you like?", Response: "We love the precious. Yummy food we like, raw fish, rabbits, all of them.<br> I like them raw and raddeling. Yes precious raw we like them" };
var Question6 = { Question: "What don't you like?", Response: "Filthy orcsisses, stupid fat hobbitsses.<br> Yes precious..  but juicy and tender they are.." };
var Question7 = { Question: "How are you?", Response: "We are so happy precious oh yees..<br> Up and down down and up.. up up up we go.. Smeagoooool!!" };


var AllQueries = [Question1, Question2, Question3, Question4, Question5, Question6];


app.post("/creature", function (req, res) {

    var aQuestion = req.param("question");
    //var BestQuestion = req.param("bestQuestion");
    var BestAnswer = req.param("bestAnswer");

    var length = AllQueries.length;

    Answer = "What does it ask us?? Gollum! Gollum!!...";

   for (var i = 0; i < length; i++) {
        if (AllQueries[i].Question === aQuestion) {
            if (BestAnswer != undefined && BestAnswer.trim().length > 0) {
                AllQueries[i].Response = BestAnswer;
            }
            Answer = AllQueries[i].Response;
        }

   }

   //check if answer was found, if it's default echo user's question:
   if(aQuestion != '' && Answer == "What does it ask us?? Gollum! Gollum!!...") {
        Answer = aQuestion;
   }


    res.sendfile("public/index.html");
Solution courtesy of: Matthew Graves

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.