Skip to content

Conversation

@esyasar
Copy link

@esyasar esyasar commented Sep 15, 2019

I asked questions on Saturday session and checked my javascript file several times, but I couldn't make it active on HTML file.

Copy link
Author

@esyasar esyasar left a comment

Choose a reason for hiding this comment

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

I worked on the js part, and I make it correct. It's working now!

@epq
Copy link
Collaborator

epq commented Sep 24, 2019

Yay, I'm glad you got your assignment to work correctly!
Good job adding many different inputs and outputs.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

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)){
Copy link
Collaborator

Choose a reason for hiding this comment

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

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" />
Copy link
Collaborator

Choose a reason for hiding this comment

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

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 = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants