Page 275 - Web Applications (803) Class 12
P. 275

document.getElementById(“result”).innerHTML = “The factorial is “+factori-
                        al;

                      }
                          </script>
                        </body>

                        </html>
                        Output:






















                   8.  Write a code in JavaScript to accept a string from the user and display the following:
                       (a) The length of the string

                       (b) Replace the alternate letters in the string with ‘#’. Display the new string in an alert box.

                  Ans.  <html>
                      <head>
                          <title>String Operations</title>

                      </head>
                      <body>

                          <h1>Operations on String</h1>
                          <p>Enter a string:</p>
                          <input type=”text” id=”Input”>
                          <button onclick=”calculate()”>Click Here</button>

                          <script>
                              function calculate() {

                                  var str1 = document.getElementById(“Input”).value;
                                  var length = str1.length;               //length of the string
                                  // Replace alternate letters with ‘#’

                                  var nstr = “”;
                                  for (var i = 0; i < length; i++) {
                                      if (i % 2 == 0)

                                          nstr += str1[i];

                                                                                                    Practical Work   273
   270   271   272   273   274   275   276   277   278   279   280