From 2c75ae3043f9e7b292c2d6bfd2ec3344816f0c9a Mon Sep 17 00:00:00 2001 From: inanfatih Date: Sun, 15 Sep 2019 18:46:19 -0400 Subject: [PATCH] assignment completed --- README.md | 25 ------------------------- index.html | 1 + script.js | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 script.js diff --git a/README.md b/README.md index 0e3e294..ecacdcc 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,4 @@ -# Week8 - Chatbot -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' } diff --git a/index.html b/index.html index 4793879..4eaf969 100644 --- a/index.html +++ b/index.html @@ -2,6 +2,7 @@ My First Chatbot + diff --git a/script.js b/script.js new file mode 100644 index 0000000..0d17a3c --- /dev/null +++ b/script.js @@ -0,0 +1,41 @@ +//object containing inputs and outputs arrays +let chatInputsOutputs = { + inputs: ['Hello', 'How are you?', 'What is your favourite colour?'], + outputs: [ + 'Hi', + 'Great!', + "I have so many favorites it's hard to choose one.", + ], +}; +console.log(chatInputsOutputs); +console.log(chatInputsOutputs.inputs); +console.log(chatInputsOutputs.outputs); + +function reply() { + let question = document.getElementById('input').value.toLowerCase(); + + console.log(question); + + // statement to answer the question based on the index of the question. + if ( + question.toLowerCase() && + chatInputsOutputs.inputs + .map(v => v.toLowerCase()) + .includes(question.toLowerCase()) + ) { + let indexOfQuestion = chatInputsOutputs.inputs + .map(v => v.toLowerCase()) + .indexOf(question.toLowerCase()); + document.getElementById('output').value = + chatInputsOutputs.outputs[indexOfQuestion]; + } else { + //when the input is not recognized + document.getElementById('output').value = + "I don't understand that command. Please enter another."; + } +} + +document.getElementById('submit').addEventListener('click', function() { + console.log('submit clicked'); + reply(); +});