Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Why Won't this Output 100%

I am trying to make a progress bar, but I am trying to figure out the percentage. I have two textboxes Make sure to put the larger number in the second and it will calculate the percentage by dividing

$("#btn").click(function () {
  var num1 = $("#textbox1").val();
  var num2 = $("#textbox2").val();

  var perectageTotal = (num1 / num2) * 100;

  let totalString = perectageTotal.toString().length;

  if (totalString > 2) {
    var result = perectageTotal.toString().slice(0, 2);
  } else if ((totalString = 1)) {
    var result = "100";
  } else {
    var result = perectageTotal.toString().slice(0, 4);
  }
  console.log(parseInt(result));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="textbox1" id="textbox1" placeholder="Enter Number 1" />
<input type="textbox2" id="textbox2" placeholder="Enter Number 2" />
<input type="button" value="Click Me" id="btn" />

them, it works but dividing the same number to get 100% does not work, it only output 10. Why?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

dividing the same number to get 100% does not work, it only out put 10. Why??

You are slicing off the end here: var result = perectageTotal.toString().slice(0, 2);

You can replace that 2 with a 4 to get your expected result.

$("#btn").click(function() {
  var num1 = $("#textbox1").val();
  var num2 = $("#textbox2").val();

  var perectageTotal = (num1 / num2) * 100;

  let totalString = perectageTotal.toString().length;
  if (totalString > 2) {
    var result = perectageTotal.toString().slice(0, 4);
  } else if ((totalString = 1)) {
    var result = "100";
  } else {
    var result = perectageTotal.toString().slice(0, 4);
  }
  console.log(parseInt(result));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="textbox1" id="textbox1" placeholder="Enter Number 1" />
<input type="textbox2" id="textbox2" placeholder="Enter Number 2" />
<input type="button" value="Click Me" id="btn" />

The block of logic at the end seems to do nothing. Also don't use var anymore and extract your code into a function so you can explain what it does.

You can Math.floor, Math.ceil or Math.round to always round down to the nearest integer, always round up to the nearest integer, or to round to the nearest integer respectively.

function divisionToPercentageString(numerator, denominator) {
  return Math.round(numerator / denominator * 100).toString() + '%';
}

$("#btn").click(function() {
  const num1 = $("#textbox1").val();
  const num2 = $("#textbox2").val();

  const result = divisionToPercentageString(num1, num2);
  console.log(result);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="textbox1" id="textbox1" placeholder="Enter Number 1" />
<br />
<input type="textbox2" id="textbox2" placeholder="Enter Number 2" />
<br />
<input type="button" value="Click Me" id="btn" />

about 4 years ago · Juan Pablo Isaza Report

0

In your first if statement

if (totalString > 2) { var result = perectageTotal.toString().slice(0, 2); }

'100' has a length of 3 and will therefore fall into this category and only take the first 2 characters.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!