Friday, 22 November 2013

Logical Pattern Questions And Answers in JS

Like:-

 54321
   4321
     321
       21
         1

Ans:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

     <script type="text/javascript">
         function f1() {
             var num = document.getElementById("txtNum").value;
             for (var i = 1; i <= num; i++) {
                 for (var sp =1 ; sp <= i; sp++) {
                     document.write("&nbsp;");
                 }
                 for (var j = num - i + 1; j >= 1; j--) {
                     document.write(j);
                 }
                 document.writeln("<br/>");
             }

         }
  
    </script>
</head>
<body>
<input id="txtNum" type="text" />
    <input id="btnPattern" type="button" value="button" onclick="f1()"/>

</body>
</html>

No comments:

Post a Comment