Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

mixing function in arrays

              //HTML

    <button type="button" class="input" id="submit" name="button">ask</button>

    <input class="ask" type="text" class="input" id="cuestion" name="name" value="">

     <p class="aiAnswer" id="texBox"></p>
          let input = document.querySelector('#cuestion');

          let button = document.querySelector('#submit');

          let pAI = document.querySelector(".aiAnswer");

        let array = [
          {
            newQuestion : "what is your name",
            newAnswer : "i do not have a name"
          },
          {
            newQuestion : "hi",
            newAnswer : "hello"
          },
          {
            newQuestion : "what are you",
            newAnswer : "i dont know"
          },
          {
            newQuestion : "what can you do",
            newAnswer : "what ever you teach me to do"
          },
          {
            newQuestion : "how old are you",
            newAnswer : "i dont age"
          }
      ];


button.addEventListener("click", () =>{

      for (var i = 0; i < array.length; i += 1)
          {
                let x = array[i];
                let value = input.value;
                if (value == "") {
                       return;
                }else  if( value.toLowerCase() === (x.newQuestion ).toLowerCase()){

                          pAI.innerHTML = x.newAnswer;
                          // break;

                 }else { // this runs don't matter what the the top statement does 
                       alert("what ever");
                 }

          }

})

//the problem that i am having is that the alert(); keep activating even if the first statement is correct // i want to learn how arrays work 100% before i move jQuery.

1 Answer

Steven Parker
Steven Parker
243,253 Points

Without the HTML part of the code, only a partial analysis can be done. But even so, much of the issue is clear:

This code does not compare the user input with the answer, but instead compares it with the question. So the only correct answer to the first prompt is "what is your name".

And since both conditions include a "break", the loop will never repeat.

first thanks for checking my code. I don't want it to repeat I just want it to check if the answer = question if not to do something it is giving me the answer but it also is activating th else if () statement

Steven Parker
Steven Parker
243,253 Points

When the first "if" test fails, then the code in the "else" section will run. The second "if" (the one after "else") does not do anything since it is testing for the exact opposite of the condition of the first "if".

Also, do you really want to prompt with the word "ask" instead of using a question from the array?

just updated the code on top man may be u can get a better idea of what am doing. am just practicing arrays that's why im doing this.

Steven Parker
Steven Parker
243,253 Points

I'm confused about your intentions, and some of the code is not shown here. All of the HTML is missing, and so are the parts of the script that define "button" and "input".

Do you have a new question, now that you have made changes to the code?

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

hey man thanks for that video it was very helpful, just updated my code see i only want the alert to pop up only if the the " if " statement == false if i take the "else" statement away it works fine but the point is to learn how to do everything not just what is easy.