-
Notifications
You must be signed in to change notification settings - Fork 10
My Chatbot #11
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: Elif-Sude-Yasar
Are you sure you want to change the base?
My Chatbot #11
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
|
|
||
| // I created a variable to hold my index and initialized it as an object. | ||
| var myObject = { | ||
| input: ["hello","how are you?", "what is your favorite color?"], | ||
| output: ["Hi", "Great!", "I have so many favorites it's hard to choose one."] | ||
| }; | ||
|
|
||
| //This is for to check that I assigned the values correctly. | ||
| console.log(myObject); | ||
|
|
||
|
|
||
| function reply(){ | ||
| // I accessed object property. | ||
|
|
||
| var question = document.querySelector("#oranges").value.toLowerCase(); | ||
| console.log(question); | ||
|
|
||
| // I wrote a conditional statement to check if the value matches with question(input) and answer(output). | ||
| if(myObject.input.includes(question)){ | ||
|
Collaborator
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. This is very good. You can also only use the Here's an example of how to do that: const index = myObject.input.indexOf(question);
if (index !== -1) {
let replyUser = myObject.output[index];
document.querySelector("#output").textContent = replyUser;
} else {
document.querySelector("#output").textContent = "I don't understand that command. Please enter another";
}Another thing is that you should be mindful of adding whitespace when writing JavaScript to make sure your code is easy to read. Generally it helps to improve readability of your code if you follow these rules for adding whitespace: see here. You should be able to format your code with your code editor so you don't have to add the spacing manually. For example, line 19 without spacing: if(myObject.input.includes(question)){With spacing added (note the space added between the if (myObject.input.includes(question)) { |
||
| let i = myObject.input.indexOf(question); | ||
| let replyUser = myObject.output[i]; | ||
| document.querySelector("#output").textContent = replyUser; | ||
| }else{ | ||
| //If user enter a different value, it will show "I don't understand that command. Please enter another". | ||
| document.querySelector("#output").textContent = "I don't understand that command. Please enter another"; | ||
| } | ||
| } | ||
| reply | ||
|
Collaborator
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. You can remove line 28 since you aren't using this value for anything. |
||
| //I attached an event to the button. That way, my js will work. | ||
| document.getElementById("submit").addEventListener("click", function() {reply()}); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,22 @@ | |
| <head> | ||
| <title>My First Chatbot</title> | ||
| <link rel="stylesheet" href="style.css"> | ||
| <script src="chatbot.js" defer></script> | ||
|
|
||
| </head> | ||
|
|
||
| <body> | ||
| <div id="demo"> | ||
| <h1>My first chatbot!</h1> | ||
| <h3>Talk to your bot!</h3> | ||
| <input id="input" type="text" placeholder="Say something" /> | ||
| <input id="oranges" type="text" placeholder="Say something" /> | ||
|
Collaborator
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. Was this line just for testing? |
||
| <button id="submit">Submit</button> | ||
| <h3>Chat history</h3> | ||
| <div id="chatBot"> | ||
| <textarea id="output" name="ChatHistory" rows="15"></textarea> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| </body> | ||
|
|
||
| </html> | ||
| </html> | ||
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.
You can use
varif you like but there are some issues with usingvarso generally, it's preferred to useconstorlet. Here's an article that explains some of the issues with using var.