Conversation
esyasar
left a comment
There was a problem hiding this comment.
I worked on the js part, and I make it correct. It's working now!
|
Yay, I'm glad you got your assignment to work correctly! You can also organize your code into different folders to keep CSS, and JavaScript in different folders, see this link for more information. |
| document.querySelector("#output").textContent = "I don't understand that command. Please enter another"; | ||
| } | ||
| } | ||
| reply |
There was a problem hiding this comment.
You can remove line 28 since you aren't using this value for anything.
| 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)){ |
There was a problem hiding this comment.
This is very good. You can also only use the indexOf array method instead of using both includes and indexOf. indexOf returns -1 if the value is not found in the array so you can use this to check if the input is valid and get the index of the input.
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 keyword and the ( and the ) and the {
if (myObject.input.includes(question)) {| <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" /> |
There was a problem hiding this comment.
Was this line just for testing?
Anyway, it's a good idea to give your ids meaningful names 😄
| @@ -0,0 +1,30 @@ | |||
|
|
|||
| // I created a variable to hold my index and initialized it as an object. | |||
| var myObject = { | |||
There was a problem hiding this comment.
You can use var if you like but there are some issues with using var so generally, it's preferred to use const or let. Here's an article that explains some of the issues with using var.
I asked questions on Saturday session and checked my javascript file several times, but I couldn't make it active on HTML file.