Page 377 - Web_Application_v2.0_C12_Fb
P. 377

Output of the preceding code is as follows:


















                   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 ‘#’.

                   Ans.  <!DOCTYPE HTML>
                      <HTML>
                      <HEAD>
                      <TITLE>Operations on String</TITLE>
                      </HEAD>

                      <BODY>
                      <H1>Operations on String</H1>
                      <SCRIPT>
                      function calculate() {
                      var str1 = document.getElementById("Input").value;
                      var length = str1.length;
                      var nstr = "";
                      for (var i = 0; i < length; i++) {

                      if (i % 2 == 0)
                      nstr += str1[i];
                      else
                      nstr += "#";
                      }
                      document.write("Length of the string: "+ length + "<BR> Modified string: " +
                         nstr);
                      }
                      </SCRIPT>
                      <P>Enter a string:</P>
                      <INPUT TYPE="text" ID="Input">
                      <BUTTON ONCLICK="calculate()">Click Here</BUTTON>
                      </BODY>

                      </HTML>




                                                                                                    Practical Work   375
   372   373   374   375   376   377   378   379   380   381   382