Hello readers, in this article we will discuss a question i.e. Write a Javascript program to calculate the simple interest using principal, rate, and time.

Write a Javascript program to calculate the simple interest using principal, rate, and time.

Write a Javascript program to calculate the simple interest – Solution

In order to calculate the simple interest using JavaScript, there are basically two approaches. They are the following:

  • Using HTML form
  • Using JavaScript Prompt

    Using HTML form

    In order to calculate the simple interest using principal, rate, and time, we first should access the form data inside JavaScript. So, we first will create an HTML file named “index.html”. And the HTML file will contain the following code.

    <html>
      <head>
        <title>Calculate Simple Interest </title>
      </head>
      <body>
        <form name="myForm" method="POST" onsubmit="onSubmit()">
          <input
            type="number"
            name="principal"
            placeholder="Enter Principal amount..."
          />
          <input type="number" name="time" placeholder="Enter Time..." />
          <input type="number" name="rate" placeholder="Enter Rate..." />
          <input type="submit" />
        </form>
        <script src="./script.js"></script>
      </body>
    </html>

    Now let’s discuss the code. The code includes a form called “myForm” which can be used later in a JavaScript file to access the data. The form contains three input forms and a submit button. And at the end, a JavaScript file named “script.js” is linked with the HTML file.

    Now, you are done with the HTML file, we can now proceed with the JavaScript file. We will name the file “script.js” inside the same folder as of “index.html”. The JavaScript file will contain the following code.

    function onSubmit(){
    const principal = document.myForm.principal.value;
    const time = document.myForm.time.value;
    const rate = document.myForm.rate.value;
    alert((principal * time * rate)/100);
    }

    The following is the output shown in the browser.

    On adding data
    After Submit

    Using JavaScript Prompt

    Unlike the previous method, in this method, we should not create an HTML form. Since we are going to run the code in Browser, we will need an HTML file that just links to the JavaScript file.

    We will first create an HTML file named “index.html” and the following will be the code inside that file.

    <html>
    <head>
    <title>Calculate Simple Interest</title>
    </head>
    <body>
    <script src="./script.js"></script>
    </body>
    </html>

    The HTML file just contains a link to the JavaScript file named “script.js”. And the JavaScript file will contain the following code.

    const principal = parseInt(prompt("Enter Principal"));
    const time = parseInt(prompt("Enter time"));
    const rate = parseInt(prompt("Enter rate"));
    alert((principal * time * rate)/100);

    The following images show the output in the Browser.

    Principal prompt
    Time prompt
    Rate prompt
    Output Alert

    Also, consider reading the following.

      Similar Posts

      Leave a Reply

      Your email address will not be published. Required fields are marked *