DEV Community

Michael Burrows
Michael Burrows

Posted on • Originally published at


How to create a percentage calculator using JavaScript

In this tutorial we’ll create 3 different types percentage calculators using JavaScript.

  • X is what % of Y?
  • What is X% of Y?
  • What is the % increase or decrease between X & Y?

Each calculator has almost identical HTML markup and the only difference in the JavaScript is the maths used to calculate the solution. If you just looking for a copy and paste solution a working HTML file with the 3 calculators can be downloaded from here.

1 – X is what % of Y?

Example – 50 is 20% of 250 || (50 / 250) * 100 = 20%

<form id="calc1">  
  <input id="calc1-num-x" type="number" /> is what percent of
  <input id="calc1-num-y" type="number" /> ?
  <input id="calc1-submit" type="submit" value="Calculate" />
  <input id="calc1-solution" type="number" readonly="readonly" />
Enter fullscreen mode Exit fullscreen mode
  .addEventListener("click", function (e) {
    const numX = document.getElementById("calc1-num-x").value;
    const numY = document.getElementById("calc1-num-y").value;
    const percentage = (numX / numY) * 100;
    document.getElementById("calc1-solution").value = percentage;
Enter fullscreen mode Exit fullscreen mode

2 – What is X% of Y?

Example – 50% of 250 = 125 || (50 / 100) * 250 = 125

<form id="calc2">
  What is <input id="calc2-num-x" type="number" /> % of
  <input id="calc2-num-y" type="number" /> ?
  <input id="calc2-submit" type="submit" value="Calculate" />
  <input id="calc2-solution" type="number" readonly="readonly" />
Enter fullscreen mode Exit fullscreen mode
  .addEventListener("click", function (e) {
    const numX = document.getElementById("calc2-num-x").value;
    const numY = document.getElementById("calc2-num-y").value;
    const percentage = (numX / 100) * numY;
    document.getElementById("calc2-solution").value = percentage;
Enter fullscreen mode Exit fullscreen mode

3 – What is the % increase or decrease between X & Y?

Example – 50 to 75 = 50% || (75 / 50) / 50 * 100 = 50%

<form id="calc3">
  What is the percentage increase or decrease from
  <input id="calc3-num-x" type="number" /> to
  <input id="calc3-num-y" type="number" /> ?
  <input id="calc3-submit" type="submit" value="Calculate" />
  <input id="calc3-solution" type="number" readonly="readonly" />
Enter fullscreen mode Exit fullscreen mode
  .addEventListener("click", function (e) {
    const numX = document.getElementById("calc3-num-x").value;
    const numY = document.getElementById("calc3-num-y").value;
    const percentage = (numY - numX) / numX * 100;
    document.getElementById("calc3-solution").value = percentage;
Enter fullscreen mode Exit fullscreen mode

For each of the calculators we’ve added a click event listener to the submit buttons. When these buttons are clicked the 2 numbers entered into the form are saved as variables. We then perform the percentage calculation on these numbers and output the value into the solution input field.

Top comments (0)

The JavaScript Brief

1. Top 5 MERN STACK projects to improve your practical understanding

Boost your MERN Stack development skills by undertaking interesting beginner projects. These five engaging projects cover web applications and range from social media website applications to geo-social networking maps. Hone your understanding and apply modern techniques backed up by hands-on experience.

2. How To Optimize Your React App’s Performance

Learn the best optimizing techniques to make your React applications faster and more efficient. Focusing on the identification of performance bottlenecks and common pitfalls to avoid, these optimization strategies will keep your applications running smoothly even when faced with growing complexity.

3. A story of let, const, object mutation, and a bug in my code

In the pursuit of bug-free code, explore an incident involving a mix-up between const and let, making sure your custom code works effectively with third

party documentation. Discover best practices on program flow and learn about JavaScript's unpredictable aspects to ensure your core code is robust.