Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda