Page 371 - Web_Application_v2.0_C12_Fb
P. 371

Practical Work




                                                                                                 21 st  Century   #Critical Thinking
                                                                                                     Skills  #Creativity


                   1.   Write a JavaScript program using a function that accepts a percentage from the user and checks whether
                      the percentage is greater than or equal to 40. If true, it displays 'PASS'; otherwise, it displays 'FAIL' using
                      a function and the conditional operator.

                   Ans.  <!DOCTYPE HTML>
                      <HTML>
                      <HEAD>
                      <TITLE>Result Checker</TITLE>
                      </HEAD>

                      <BODY>
                      <H1>Percentage Checker</H1>
                      <P>Enter your percentage:</P>
                      <INPUT TYPE="number" ID="pInput">
                      <BUTTON ONCLICK="check()">Check Your Result</BUTTON>
                      <P ID="result"></P>
                      <SCRIPT>
                      function check() {

                      var perc = parseFloat(document.getElementById("pInput").value);
                      var resultElement = document.getElementById("result");
                      var message = (perc >= 40) ? "PASS" : "FAIL";
                      resultElement.textContent = "Result:"+ message;
                      }
                      </SCRIPT>
                      </BODY>
                      </HTML>
                       Output of the preceding code is as follows:























                                                                                                    Practical Work   369
   366   367   368   369   370   371   372   373   374   375   376