-
Notifications
You must be signed in to change notification settings - Fork 12
Week11 chatbot #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Week11 chatbot #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 `<input>` element. | ||
| - HINT: use the id assigned to the `<input>` 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 <textarea> element. | ||
| - HINT: use the id assigned to the `<textarea>` element to get access to the element. | ||
| 2. if it does not, assign the string "I don't understand that command. Please enter another." to the **value** of the `<textarea>` element. | ||
| - HINT: use the id assigned to the `<textarea>` element to get access to the element. | ||
| 7. Below your 'reply' function, attach a 'click' event listener to the `<button>` element defined in the HTML file. | ||
| - HINT: use the id assigned to the `<button>` element to get access to the element. | ||
| 8. Save your JavaScript code. You now have a functional simple chatbot. Try it out by opening the 'index.html' file in your browser. REMEMBER to include your JavaScript code in the ‘index.html’ file either internally or externally. | ||
|
|
||
| ### SUBMISSION | ||
|
|
||
| Before submitting your assignment, remember to organize your code according to the best practices for structuring code files, as explained in the class. This means: | ||
|
|
||
| * HTML, CSS, and JS code should be in separate files | ||
| * All files should be organized into their respective folders | ||
|
|
||
| ### EVALUATION | ||
|
|
||
| When evaluating this assignment, we will be looking for: | ||
|
|
||
| * Proper code formatting | ||
| * Correct structuring of code files | ||
| * Descriptive comments within code | ||
| * Functionality of the chatbot (if it’s working or not) | ||
|
|
||
| ### BONUS | ||
|
|
||
| Try out different ‘input’ and ‘output’ statements with the chatbot. | ||
| Add your own CSS and/or restructure the HTML to your liking. Make your chatbot unique! | ||
|
|
||
| HINT: you will need these in part 2. |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <style> | ||
| button { | ||
| font-family: Helvetica; | ||
| font-size: 10pt; | ||
| /*width: 92px;*/ | ||
| } | ||
| textarea { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indenting is a big deal when css gets really big, make sure that your indentations are always proper |
||
| font-family: arial; | ||
| font-size: 10pt; | ||
| } | ||
| body { | ||
| color: #333; | ||
| background-color: #efefef; | ||
| font: 13px helvetica,arial,freesans,clean,sans-serif; | ||
| } | ||
| #demo { | ||
| width: 80%; | ||
| height: 600px; | ||
| 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; | ||
| } | ||
| #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; | ||
| } | ||
| </style> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /* | ||
| Aziz Omar | ||
| Chatbot4 | ||
| */ | ||
| // contains all inputs and outputs | ||
| let data = [{ | ||
| input: ['hi', 'hey!', 'yo yo'], | ||
| output: ['Hello', 'Hey !', 'Whats up?'] | ||
| }, | ||
|
|
||
| { | ||
| 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'], | ||
| output: ['red', 'gray', 'black'] | ||
| }, | ||
|
|
||
| { | ||
| input: ['where are you', 'are you near by', 'where are you located'], | ||
| output: ['home', 'at work', 'on vacation'] | ||
| } | ||
| ]; | ||
|
|
||
|
|
||
| /* | ||
| iterates through an array to find a question simillar to user's input | ||
| */ | ||
|
|
||
| const findSimillarQuestion = givenInput => (item) => { | ||
| // return item.input.toUpperCase().includes(givenInput.toUpperCase()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment for deleting commented out code
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also why are you no longer using toUpperCase? |
||
| return item.input.includes(givenInput); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| // repaly function | ||
| const replay = () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spelling |
||
|
|
||
| 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 { | ||
| bot = detectedQuestion.output[rndmNumber]; | ||
| } | ||
|
|
||
| } else if (question == "show me a dog") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be good if you abstracted this further and instead of checking specifically for these 2 strings you adding these questions as part of the data array |
||
| // 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); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always delete commented out code