week8 homework submission#7
Conversation
|
Very good work! You can also organize your code into different folders to keep CSS, and JavaScript in different folders, see this link for more information. |
| let question = document.getElementById("input").value.toLowerCase(); | ||
|
|
||
| //conditional statement | ||
| if (chatbot.input[0] === question) { |
There was a problem hiding this comment.
Can you think of checking for the input in a different way without all of these if and else if statements?
For example, if I had 20 possible input/outputs, that would be a lot of if statements to write!
Click here for a suggestion
-
You could loop through the chatbot's input and compare that with the user's input to check if it's valid. If there is a match, display it. Otherwise display "enter a valid value."
-
Or you could use the array indexOf() method to see if the user's input is valid. If it is, display it. Otherwise, display "enter a valid value."
|
|
||
|
|
||
| //attach a 'click' event listener to the <button> element defined in the HTML file. | ||
| document.getElementById("submit").addEventListener("click", function() {reply()}) No newline at end of file |
There was a problem hiding this comment.
For your callback function, it's not necessary to create an anonymous function (a function with no name, i.e. function() { ... }) and call reply() inside of it. It works, but you can just pass in the name of your reply function directly. Like this:
document.querySelector("#submit").addEventListener("click", reply);Or your callback function can be an anonymous function like below. If you do this, you don't need to write a separate reply function because you have included it inline:
document.querySelector("#submit").addEventListener("click", function() {
const question = document.querySelector("#input");
if(question.value === "hello"){
document.querySelector("#output").textContent = "hi";
} else{
document.querySelector("#output").textContent = "I don't understand that command. Please enter another.";
}
});| @@ -0,0 +1,33 @@ | |||
|
|
|||
| //properties added to the object: ‘input’ and ‘output’ as an array | |||
There was a problem hiding this comment.
Nice job adding comments throughout this file!
No description provided.