Write a JavaScript Program to read a number from user, store its factors into the array and display that array. (Handle onClick Event)

This Blog Post Will Show You An Project About JavaScript Program to read a number from user, store its factors into the array and display that array. (Handle onClick Event). 

Requirements : 

Suggested Source Code Editor :

    VS Code (Visual Studio Code), Atom Or Sublime and Notepad work Fine

Is Any IDE Is Needed :

    No. Any Browser Will Be fine Like Chrome, Firefox, Brave, etc.,

The Files Are Created for HTML5 Version

Code :


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible"
        content="IE=edge" />
    <meta name="viewport" 
        content="width=device-width,
        initial-scale=1.0" />
    <title>Document</title>
    <script type="text/javascript">
    function factors() {
      var n, a;
      a = [];
      n = document.my-from.txt.value;
      for (i = 1; i <= n; i++) {
        if (n % i == 0) {
          a = a + [i] + ",";
        }
      }
      document.getElementById("ans")
        .innerHTML
        = "[" + a + "]";
    }
    </script>
  </head>
  <body>
    <form name="my-from">
      Number: <input type="text"
                name="txt" />nbsp;
              <input type="button"
                value="Submit"
                onclick="factors()"
                 />
      <p id="ans"></p>
    </form>
  </body>
</html>


Comments