0

I am working on a HTML chatbot and I need a way to check the users in put to see if it's in an array. I have tried already but it doesn't see to be working. I need it to take the users greets and store it into an array. Then, when the user enters any one of the greets in the array to reply with a random value from the array.

var greet = [];
var questions = [];
var responses=[];
function greets(){
 var learn = document.getElementById("greets").value;
 if (learn == "finished"){
  document.cookie = 'greetings='+greet+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
 }else{
  greet.push(learn);
 };
};
function ques(){
 var learn = document.getElementById("ques").value;
 if (learn == "finished"){
  document.cookie = 'questions='+questions+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
 }else{
  questions.push(learn);
 };
};
function res(){
 var learn = document.getElementById("res").value;
 if (learn == "finished"){
  document.cookie = 'responses='+responses+'; expires=Wed, 1 Jan 2070 13:47:11 UTC; path=/'
 }else{
  responses.push(learn);
 };
};

var types = [{
   key: 'greet',
   data: greet
 }, {
   key: 'questions',
   data: questions
 }, {
   key: 'responses',
   data: responses
}];
function send(){
 
/* isExist method two arguements
   arr - array of values.
   str - string to be search.
*/
 
var msg = document.getElementById("msg");


if(types.indexOf( msg )>=0){
 console.log("YES");
} else {
 console.log("NO");
}

};
<!DOCTYPE html>
<html>
<head>
<script src="index.js"></script>
<div>
 <p class="user" id="user">User: </p>
 <p class="bot" id="bot">Bot: </p>
</div>
<input class="talk" id="msg" placeholder="Start Talking!"></input><button class="send" onclick="send()">Send</button>
<p>Once Finished Type Finished.</p>
<p>Enter Some Greetings.</p>
<input placeholder="Enter Here, Friend!" id="greets"></input>
<button onclick="greets()">Enter Greet</button>
<br>
<p>Enter Some Question You Would Normally Ask.</p>
<input placeholder="Enter Here, Friend!" id="ques"></input>
<button onclick="ques()">Enter question</button>
<br>
<p>Enter Some Responses.</p>
<input placeholder="Enter Here, Friend!" id="res"></input>
<button onclick="res()">Enter Responses</button>
<br />
<style>
.talk{
 float: right;
 width: 750px;
}
div{
 border: 1px solid black;
 float: right;
 width: 800px;
 height: 250px;
}
.send{
 float:right;
 background-color: lightblue;
 border-radius: 2px;
 border: 2px solid black

}
</style>


<button onclick="loadgreets()">Load Files</button>
</head>
<body>
</body>
</html>
MCC
  • 512
  • 1
  • 5
  • 23

2 Answers2

0

It looks like you need to make 2 changes:

First, var msg = document.getElementById("msg") should be var msg = document.getElementById("msg").value to get the value of the input, not the element itself

Second, if(types.indexOf( msg )>=0) would work if msg was an exact match to one of the objects in types. However, because types is an array of objects, and it looks like you want to match the key, you can do something like what's suggested in this question: Find a value in an array of objects in Javascript

function contains(msg, types){
    for (var i=0; i < types.length; i++) {
        if (types[i].key === msg) {
            return true;
        }
    }
    return false;
}

if(contains(msg, types){
    console.log("YES");
} else {
    console.log("NO");
}
Community
  • 1
  • 1
  • you need to change the clause if(types[i].key === "greet") and then make another loop over types[i].data array to match the entered greeting or refactor the types - use key as key ;) – Eduard Void Jun 15 '16 at 14:01
0

not sure what are you going to do, but maybe I will help you with this advices:

to get the msg value use document.getElementById("msg").value;

to check if greeting exists you should use:

for(var key in greet) {
    if(greet[key] == msg) {
        //do a response
        break; //no more loops are needed if we found a match
    }
}

indexOf is not working on arrays and in javascript is no native function like PHP's in_array.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Eduard Void
  • 2,646
  • 1
  • 13
  • 13