Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<!DOCTYPE HTML>
<head>
<title>My First Chatbot</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="style/style.css">
<script src="script/script.js"></script>
</head>

<body>
Expand Down
24 changes: 24 additions & 0 deletions script/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Declare input and output
const ioChatbot = {
input : "hello",
output : "hi"
};
console.log(ioChatbot);

// Reply function for getting input element
function reply(){
const question = document.querySelector("#input");
if(question.value === "hello"){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of comparing question.value with the string "hello", you should compare question.value with the input of your object, ioChatbot.

document.querySelector("#output").textContent = "hi";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should update this value with the output of ioChatbot

} else{
document.querySelector("#output").textContent = "I don't understand that command. Please enter another.";
}

console.log(question.value);
}

if(document.querySelector("#submit")){
document.querySelector("#submit").addEventListener("click", function(){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.";
   }
});

reply();
});
}
File renamed without changes.