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

the .push method adds a new object if i tried to add a new one it replaces the old one

                        <!-- HTML -->

    <input id="inputValue" type="text" name="" value="" >
    <input type="submit" onclick="getVal()">
     <output id="outValue" name="result"></output>

                          <!-- END HTML -->

     <script type="text/javascript">

function getVal() {


      const array = [
          {
            newQuestion : "hi",
            newAnswer : "hello"
          } 
      ];

        let value =  document.querySelector("#inputValue").value;
        let outValue =  document.querySelector("#outValue");

let h = "";
let ans ;

              for (let i = 0; i < array.length; i++) {
                    let g = array[i].newQuestion == value;
                    if (g == true) {
                            h =array[i].newQuestion;
                             ans = array[i].newAnswer;
                         }
                    }


            if (h == null || h == "") {
                let newAsk =  prompt("help me learn what will be the asnwer to your question");
                                       if (newAsk == null ) {
                                          return;
                                       }else {
// here i tried to add a new object and it does bout if i tried to add a new one it replaces the //old one
                                         let ar2 = new Object({ 
                                                    newQuestion : value,
                                                    newAnswer : newAsk
                                                  });
                                            array.push(ar2);
                                            console.log(array);
                                       }
              }else {
                outValue.innerHTML = ans;
                }



}

/* what i am trying to do is make it learn new words if the program does not know the question i want it to ask for the answer and add it to the array*/

1 Answer

Steven Parker
Steven Parker
243,199 Points

The "push" doesn't replace anything, it only adds on. But your array is defined inside the function, so when the function ends it goes out of scope and is disposed. Then the next time the function runs it is created again with the initial contents.

You might try moving the array outside of the function to make it a global. Then it would continue to exist between function calls and get added on to each time you "push".

thanks man