From c2a5e544686c507671b3db79dfabb3a0d4a069d8 Mon Sep 17 00:00:00 2001 From: azizck Date: Sun, 22 Sep 2019 11:52:03 -0400 Subject: [PATCH 1/4] done --- README.md | 66 +++++++++- chatbot_style.css | 102 ++++++++-------- debug.log | 305 ++++++++++++++++++++++++++++++++++++++++++++++ index.html | 2 +- js/debug.log | 61 ++++++++++ js/main.js | 75 ++++++++++++ style.css | 52 ++++++++ 7 files changed, 607 insertions(+), 56 deletions(-) create mode 100644 debug.log create mode 100644 js/debug.log create mode 100644 js/main.js create mode 100644 style.css diff --git a/README.md b/README.md index 5e9e1fd..29da157 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,65 @@ -# Chatbot +# Week8 - Chatbot -Please see the instructions [here](https://docs.google.com/document/d/1BP27Tjgit66o6kLpzu8RMnPr7M5GWpC_2N05DvMMTUE/). +See the instructions [here](https://docs.google.com/document/d/1123vh08osRkCyqsnyR0SQMC1n4l0DlDIS0OYSrPSlY0/edit?usp=sharing) for the first part of your chatbot, or read below: + +**Due Date: Sunday, September 15 at 6:00 p.m.** + +Complete the following assignment and create a pull request to GitHub for reviewing by the instructor. + +The homework assignments for the next few lectures will be interconnected. By the end of JavaScript 2, you will have developed a fully functional chatbot. These assignments will build on the previous week's assignment, therefore, it is very important that you complete the assignment in a timely manner (i.e. by the due date). + +## CHATBOT PART I + +In this first assignment, you will begin by building a very simple chatbot. As you progress through the remaining JavaScript weeks, you will add more and more functionality to the chatbot. + +You are provided the HTML and CSS code for this assignment in this repository. Your task will be to write the JavaScript portion to make the chatbot functional and interactive. Remember to add comments to your code, describing what it does. + +1. In your JavaScript code, declare a variable and initialize it as an object. +2. Add two properties to the object: ‘input’ and ‘output’. + 1. To the ‘input’ property/key assign a greeting or a question that you want the chatbot to reply to. Some examples are: + * Hello + * How are you? + * What is your favourite colour? + 2. To the ‘output’ property/key assign answers to the greetings or questions you wrote in part a. Some examples to the inputs above are: + * Hi + * Great! + * I have so many favorites it's hard to choose one. +3. console.log() your variable to confirm that you have assigned the values correctly. If done correctly, you output should look similar to: +```js +{ input: 'input1', output: 'output1' } +``` + +4. Below your variable declaration, create a function called ‘reply’. +5. In the ‘reply’ function, declare a variable called ‘question’ and assign to it the **value**of the HTML `` element. + - HINT: use the id assigned to the `` element to get access to the element. +6. Use a conditional statement to check if the value you stored in the 'question' variable matches the 'input' defined in the object you first created. + 1. If it does, assign the corresponding output to the **value** of the -
+ + + diff --git a/js/debug.log b/js/debug.log deleted file mode 100644 index a0df5ae..0000000 --- a/js/debug.log +++ /dev/null @@ -1,61 +0,0 @@ -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.158:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.159:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report -[0917/191312.160:ERROR:crash_report_database_win.cc(469)] failed to stat report diff --git a/js/main.js b/js/main.js index ed4d103..948a3dd 100644 --- a/js/main.js +++ b/js/main.js @@ -4,22 +4,22 @@ Chatbot2 */ // contains all inputs and outputs let data = [{ - input: ['hi','hey!','yo yo'], + input: ['hi', 'hey!', 'yo yo'], output: ['Hello', 'Hey !', 'Whats up?'] }, { - input: ['how are you?','how is it going?','how are you doing?'], + input: ['how are you?', 'how is it going?', 'how are you doing?'], output: ['good', 'not bad', 'doing well'] }, { - input: ['what is your favourite color?','what color do you prefer','tell me what color you like'], + input: ['what is your favourite color?', 'what color do you prefer', 'tell me what color you like'], output: ['red', 'gray', 'black'] }, { - input: ['where are you','are you near by','where are you located'], + input: ['where are you', 'are you near by', 'where are you located'], output: ['home', 'at work', 'on vacation'] } ]; @@ -30,44 +30,81 @@ iterates through an array to find a question simillar to user's input */ const findSimillarQuestion = givenInput => (item) => { - // return item.input.toUpperCase().includes(givenInput.toUpperCase()); - return item.input.includes(givenInput.toLowerCase()) ; + // return item.input.toUpperCase().includes(givenInput.toUpperCase()); + return item.input.includes(givenInput); } // repaly function const replay = () => { + let bot; let question = document.getElementById("input").value; + question = question.toLowerCase(); let txtArea = document.getElementById("output"); let rndmNumber = Math.floor(Math.random() * data[0].output.length); let detectedQuestion = data.filter(findSimillarQuestion(question))[0]; + let img = document.getElementById('img'); + // remove the src from img to hide it + img.src = ""; + // display textarea + txtArea.style.display = 'block'; if (detectedQuestion) { - let maxAnswer= detectedQuestion.output.sort((a,b)=> a.length - b.length )[detectedQuestion.output.length-1]; - let minAnswer= detectedQuestion.output[0]; - console.log(minAnswer) - - if (document.getElementById('shortest').checked){ - bot=minAnswer; - } - else if (document.getElementById('longest').checked){ - bot=maxAnswer; - } - else{ + let maxAnswer = detectedQuestion.output.sort((a, b) => a.length - b.length)[detectedQuestion.output.length - 1]; + let minAnswer = detectedQuestion.output[0]; + console.log(minAnswer) + if (document.getElementById('shortest').checked) { + bot = minAnswer; + } else if (document.getElementById('longest').checked) { + bot = maxAnswer; + } else { bot = detectedQuestion.output[rndmNumber]; - console.log(bot) } - } else{ - bot = " I do not understand the command "; - } - // assign the output to teaxtarea - txtArea.innerHTML +=`You: ${question}\nbot: ${bot}\n\n` ; + } else if (question == "show me a dog") { + // hide txt area + txtArea.style.display = 'none'; + // call function to display an image of a dog + displayAdog(); + bot = "an image of dog is displayed" + } else if (question == "set an alarm") { + bot = "ok !"; + delayedAlert(); + } else { + bot = " I do not understand the command "; + } + // assign the output to teaxtarea + txtArea.innerHTML += `You: ${question}\nbot: ${bot}\n\n`; } // end of replay + +// show alert in 3seconds +function delayedAlert() { + setTimeout(function () { + alert("Did you forget about me? It’s your friend, the Alarm"); + }, 3000) +} + +//diplay an image of a dog +function displayAdog() { + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function () { + if (this.readyState == 4 && this.status == 200) { + let img = document.getElementById("img"); + img.src = (JSON.parse(this.responseText)).message; + } + } + + xhttp.open("GET", "https://dog.ceo/api/breeds/image/random", true); + xhttp.send(); + + +} + + document.getElementById("submit").addEventListener('click', replay); \ No newline at end of file diff --git a/style.css b/style.css index b4c8731..877bf06 100644 --- a/style.css +++ b/style.css @@ -27,6 +27,8 @@ body { line-height: 1.4em; font: 13px helvetica, arial, freesans, clean, sans-serif; color: black; + height: 600px; + background: red; } #demo input { From c73df41df55cba2f863e72d3610b608ce2386033 Mon Sep 17 00:00:00 2001 From: azizck Date: Sun, 6 Oct 2019 19:02:20 -0400 Subject: [PATCH 4/4] organized directories --- chatbot_style.css => css/chatbot_style.css | 98 +++++++++++----------- index.html | 2 +- js/main.js | 2 +- style.css | 54 ------------ 4 files changed, 51 insertions(+), 105 deletions(-) rename chatbot_style.css => css/chatbot_style.css (95%) mode change 100755 => 100644 delete mode 100644 style.css diff --git a/chatbot_style.css b/css/chatbot_style.css old mode 100755 new mode 100644 similarity index 95% rename from chatbot_style.css rename to css/chatbot_style.css index f13c5f9..a35a93b --- a/chatbot_style.css +++ b/css/chatbot_style.css @@ -1,50 +1,50 @@ - \ No newline at end of file diff --git a/index.html b/index.html index 74ac3e0..3fe3b88 100755 --- a/index.html +++ b/index.html @@ -1,7 +1,7 @@ My First Chatbot - + diff --git a/js/main.js b/js/main.js index 948a3dd..f4911f9 100644 --- a/js/main.js +++ b/js/main.js @@ -1,6 +1,6 @@ /* Aziz Omar -Chatbot2 +Chatbot4 */ // contains all inputs and outputs let data = [{ diff --git a/style.css b/style.css deleted file mode 100644 index 877bf06..0000000 --- a/style.css +++ /dev/null @@ -1,54 +0,0 @@ -button { - font-family: Helvetica; - font-size: 10pt; - width: 92px; -} - -textarea { - font-family: arial; - font-size: 10pt; -} - -body { - color: #333; - background-color: #efefef; - font: 13px helvetica, arial, freesans, clean, sans-serif; -} - -#demo { - width: 80%; - max-width: 1000px; - margin-left: auto; - margin-right: auto; - padding: 20px; - background-color: #F8F8F8; - border: 1px solid #ccc; - box-shadow: 0 0 10px #999; - line-height: 1.4em; - font: 13px helvetica, arial, freesans, clean, sans-serif; - color: black; - height: 600px; - background: red; -} - -#demo input { - padding: 8px; - font-size: 14px; - border: 1px solid #ddd; - width: 400px; -} - -#demo textarea { - padding: 8px; - font-size: 14px; - border: 1px solid #ddd; - width: 800px; -} - -input:focus { - outline: none; -} - -textarea:focus { - outline: none; -} \ No newline at end of file