JavaScript Practice Questions And Answer

Problem: Write a program to input text from user and perform following operation
1.create an array
2.Add element into array
3.Add elements b/w array ( Ex:-in b/w position 3 & 4)
4.Remove element from an arry to is specific position
5.Display using button.
Solution:
            <html>
             <head>
              <script>
              function demo1()
             {
               let fruits = ["Apple", "Banana", "Chikoo", "Guava"]
               document.write(fruits)
               document.write("<br>"+fruits[0])
                        let f1 = fruits[4]
                        document.write("<br>"+f1)
                        //get all data of array one by one
                          for(i=0;i<=3;i++)
                        {
                            document.write("<br>"+i+fruits[i])
                        }
                        //change value at a position in array
                        fruits[2] = "Mango"
                        document.write("<br>"+fruits)
                        fruits.push("PineApple")
                        document.write("<br>"+fruits)
                        fruits[5]="Orange"
                        document.write("<br>"+fruits)
                        document.write("<br> Count "+ fruits.length)
                      //insert new element in between
                        fruits.splice(3, 0, "Graps")
                        document.write("<br>"+fruits)
                      //remove from Last
                        let f2 = fruits.pop()
                        document.write("<br>Removed from array "+f2)
                        document.write("<br>"+fruits)
                      //remove element from a position
                        fruits.splice(4,1)
                        document.write("<br>"+fruits)
                     }
               </script>
            </head>
            <body>
                <input type="button" value="Go" onclick="demo1()"/><br>
            </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!