JavaScript Practice Questions And Answer

Problem: Write a program to insert an element in an array.
Solution:
               <html>
                 <head>
                   <script>
                    var fruits = ["Apple", "Banana"]
                    function addFruit()
                    {
                        var fruitName=document.getElementById("input1").value
                        fruits.push(fruitName)
                        document.getElementById("input1").value = ""
                    }
                    function display()
                    {
                       let resultDiv = document.getElementById("result")
                       resultDiv.innerHTML = ""
                       for(f of fruits)
                       {
                         resultDiv.innerHTML = resultDiv.innerHTML + "<br>" + f
                       }
                    }
                     </script>
                   </head>
                      <body>
                       <input type="text" placeholder="fruit name" id="input1"/>
                       <br>
                       <input type="button" value="Add fruit" onclick="addFruit()"/><br>
                       <br>
                       <input type="button" value="Display" onclick="display()"/><br>
                       <div id="result" style="background-color: yellow;">
                        fruits....
                       </div>
                      </body>
                 </html>
          

Contact Us

Our Address

Office no.- 401,Shekhar Central Building ,Palasiya, Pin-code:452001, Indore

Email Us

contact@codebetter.in

Call Us

+91 88230 75444, +91 99939 28766

Loading
Your message has been sent. Thank you!