From 5550e3683a0c099f75a7e09c94b77bceae363680 Mon Sep 17 00:00:00 2001 From: esyasar Date: Sun, 15 Sep 2019 17:46:37 -0400 Subject: [PATCH 1/2] My Chatbot --- chatbot.js | 28 ++++++++++++++++++++++++++++ index.html | 7 +++++-- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 chatbot.js diff --git a/chatbot.js b/chatbot.js new file mode 100644 index 0000000..b22a36c --- /dev/null +++ b/chatbot.js @@ -0,0 +1,28 @@ + +// I created a variable to hold my index and initialized it as an object. +var myObject = { + input: ["Hello","How are you?", "What is your favorite color?"], + output: ["Hi", "Great!", "I have so many favorites it's hard to choose one."] +}; + +//This is for to check that I assigned the values correctly. +console.log(myObject); + + +function reply(){ + // I accessed object property. + + var question = document.querySelector("#input").value.toLowerCase(); + + // I wrote a conditional statement to check if the value matches with question(input) and answer(output). + if(object.input.includes(question)){ + let i = myObject.input.indexOf(question); + document.querySelector("#output").textContent = myObject[i]; + }else{ + //If user enter a different value, it will show "I don't understand that command. Please enter another". + document.querySelector("#output").textContent = "I don't understand that command. Please enter another"; + } +} + +//I attached an event to the button. That way, my js will work. +document.getElementById("submit").addEventListener("click", reply); diff --git a/index.html b/index.html index 4793879..6dfdce1 100644 --- a/index.html +++ b/index.html @@ -2,6 +2,8 @@ My First Chatbot + + @@ -13,8 +15,9 @@

Talk to your bot!

Chat history

-
+ + - \ No newline at end of file + From 4881e1e4598b9ec9fb87ac8746a3c6663935268c Mon Sep 17 00:00:00 2001 From: esyasar Date: Sat, 21 Sep 2019 15:41:37 -0400 Subject: [PATCH 2/2] chatbot homework correction --- chatbot.js | 14 ++++++++------ index.html | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/chatbot.js b/chatbot.js index b22a36c..c8fe684 100644 --- a/chatbot.js +++ b/chatbot.js @@ -1,7 +1,7 @@ // I created a variable to hold my index and initialized it as an object. var myObject = { - input: ["Hello","How are you?", "What is your favorite color?"], + input: ["hello","how are you?", "what is your favorite color?"], output: ["Hi", "Great!", "I have so many favorites it's hard to choose one."] }; @@ -12,17 +12,19 @@ console.log(myObject); function reply(){ // I accessed object property. - var question = document.querySelector("#input").value.toLowerCase(); + var question = document.querySelector("#oranges").value.toLowerCase(); + console.log(question); // I wrote a conditional statement to check if the value matches with question(input) and answer(output). - if(object.input.includes(question)){ + if(myObject.input.includes(question)){ let i = myObject.input.indexOf(question); - document.querySelector("#output").textContent = myObject[i]; + let replyUser = myObject.output[i]; + document.querySelector("#output").textContent = replyUser; }else{ //If user enter a different value, it will show "I don't understand that command. Please enter another". document.querySelector("#output").textContent = "I don't understand that command. Please enter another"; } } - +reply //I attached an event to the button. That way, my js will work. -document.getElementById("submit").addEventListener("click", reply); +document.getElementById("submit").addEventListener("click", function() {reply()}); diff --git a/index.html b/index.html index 6dfdce1..2f44d8f 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,7 @@

My first chatbot!

Talk to your bot!

- +

Chat history