Page 381 - Web_Application_v2.0_C12_Fb
P. 381

12.   Write JavaScript code to extract “apple” from the string “pineapple”.

                   Ans.  <!DOCTYPE html>
                      <HTML>
                      <HEAD>
                      <TITLE>Working with strings</TITLE>
                      </HEAD>
                      <BODY>

                      <H1>Extracting a substring from the string "pineapple"</H1>
                      <SCRIPT>
                      var str1 = "pineapple";
                      var nstr = str1.substring(4, 9);
                      document.write("The extracted string is "+"<U>"+nstr+"</U>");
                      </SCRIPT>
                      </BODY>
                      </HTML>
                       Output of the preceding code is as follows:
























                   13.  Write JavaScript code to do the following:
                       a. Create an array of five fruits.

                       b. Add the fruit “Dragonfruit” to the array.
                       c. Display the entire array.

                   Ans.  <!DOCTYPE html>

                      <HTML>
                      <HEAD>

                      <TITLE>Working with Arrays</TITLE>
                      </HEAD>
                      <BODY>

                      <H1>Working with Arrays</H1>

                      <SCRIPT>

                                                                                                    Practical Work   379
   376   377   378   379   380   381   382   383   384   385   386