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

13.  What is NaN? Explain using an example.                                        [CBSE COMPTT 2023]
               Ans.  NaN, which stands for “Not a Number,” is a special JavaScript value that represents an invalid numeric value. It can
                    result from a variety of operations, such as dividing by zero, computing the square root of a negative number, or
                    parsing a string that cannot be transformed to a number.
                     Example: const ans = 30 / 0;

                    document.write(result); // will result in NaN
                     NaN can also be used to check if a value is a number using the isNaN() functionIn JavaScript, NaN is a helpful
                    value for handling errors and unexpected results.

                 14.  Explain the difference between for and while loops in JavaScript with an example.
               Ans.
                                         for loop                                        while loop
                     The number of iterations is known beforehand.    The number of iterations is not known, and the loop
                                                                      continues as long as a specified condition is true.
                     The initialization, condition, and iteration statements  Only the condition is provided at the beginning of the
                     are provided at the beginning of the loop.       loop,  initialization  and  iteration  are  handled  outside
                                                                      the loop.
                     // Print namaste 5 times using a for loop // Print namaste 5 times using a for loop
                              for (var i = 1; i <= 5; i++)            var i = 1;

                     document.write(“Namaste”);                       while (i <= 5) {
                                                                          document.write(“Namaste”);
                                                                          i++;
                                                                      }

                                                                                                    Computational Thinking
              C.   Competency-based/Application-based questions:
                  1.   A JavaScript array is initialized as follows:

                     even=[2,4,6,8,10];
                     Write the status of the array after each of the following operations:

                    i.  even.shift();
                    ii.  even.pop();
                    iii.  even.push(12);
                    iv.  even.unshift(100);
               Ans.  i.  4,6,8,10

                     ii.  4,6,8
                     iii. 4,6,8,12
                     iv.  100,4,6,8,12
                  2.   Rachna has created an external JavaScript file named “ext_code.js” to include in her webpage. Write code to link
                    the above file in the webpage’s HTML code. Also, mention where the code should be placed in the HTML code.

               Ans.  To include an external JavaScript file, Rachna should use the script tag with the attribute src.

                    <script type="text/javascript" src="ext_code.js"></script>
                     This script tag should be included between the <head> tags in the HTML document.



                226   Touchpad Web Applications-XII
   223   224   225   226   227   228   229   230   231   232   233