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

177
Visualizações
Problems with pasrseInt()

I cant seem to work out why parseInt() is not working properly in my code when I try to pass numbers through to my function parameters. It seems to keep coming up with NaN both in my array and the function return value.

What I am trying to do is when values are keyed into the input fields the index percentage of these two values appears in the HTML and stores the value in an array. I am using a function for this because I want to have over twenty outlet calculations. I am also struggling with refactoring this code as I don't have a lot of experience with JS. Any help would be really appreciated.
Here is my HTML.

function outletIndex(actual, design) {
    
    let result1 = actual / design * 100;
    var indexArray = [];
    indexArray.push(result1);
    console.log(indexArray, result1);
    if (!isNaN(result1)) {
    document.getElementById("percentage").textContent = `${result1.toFixed(1)}%`;
    
  }
}
  const x = parseInt(document.getElementById("outlet_actual_1" ).valueAsNumber);
  const y = parseInt(document.getElementById("outlet_design_1").valueAsNumber);
  
  const outlet1Index = outletIndex(x, y);
 <table>
      <tr>
        <div class="form-group row 1" id="outlets1">
          <td><label
            >Outlet Design</label>
            <input
              name = "outlet 1 design"
              class="form-control design_1"
              id="outlet_design_1"
              type="number"
              placeholder="Outlet 1 Design"
              
            />
          </td>
          <td><label
            >Outlet Actual</label>
            <input
            name="outlet 1 actual"
              class="form-control actual_1"
              id="outlet_actual_1"
              type="number"
              placeholder="Outlet 1 Actual"
              onblur="outletIndex();"
            />
          </td>
          <td><label
            >Outlet Balance</label>
            <input
              name="outlet_balance"
              class="form-control"
              input value=""
              id="outlet_balance_1"
              type="text" 
              placeholder="Outlet 1 Balance"        
            />
          </td><td>
            <div class="proportion" id="percentage">

            </div>
          </td>
        </div>
      </tr>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The main problem here is the code that reads x and y runs before any valuie is in them.

The second problem is when you blur the control with id outlet_actual_1 you call your method, but with no arguments, when it expects actual and design to be passed in as arguments.

Finally if using valueAsNumber there's no need to use parseInt. If you do want to use it then read the value and make sure you use the radix like parseInt(document.getElementById("outlet_actual_1" ).value, 10)

The simple way to get this going is to move the code that reads x and y inside the method and remove the arguments, but there's probably more to it than that, I suspect you want to recalculate any time either field changes so lets add that too:

function outletIndex() {
    const actual = document.getElementById("outlet_actual_1" ).valueAsNumber || 0 ;
    const design = document.getElementById("outlet_design_1").valueAsNumber || 0 ;
    let result1 = actual / design * 100;
    var indexArray = [];
    indexArray.push(result1);
    console.log(indexArray, result1);
    if (!isNaN(result1)) {
    document.getElementById("percentage").textContent = `${result1.toFixed(1)}%`;
    
  }
}
<table>
      <tr>
        <div class="form-group row 1" id="outlets1">
          <td><label
            >Outlet Design</label>
            <input
              name = "outlet 1 design"
              class="form-control design_1"
              id="outlet_design_1"
              type="number"
              placeholder="Outlet 1 Design"
              onblur="outletIndex();"
            />
          </td>
          <td><label
            >Outlet Actual</label>
            <input
            name="outlet 1 actual"
              class="form-control actual_1"
              id="outlet_actual_1"
              type="number"
              placeholder="Outlet 1 Actual"
              onblur="outletIndex();"
            />
          </td>
          <td><label
            >Outlet Balance</label>
            <input
              name="outlet_balance"
              class="form-control"
              input value=""
              id="outlet_balance_1"
              type="text" 
              placeholder="Outlet 1 Balance"        
            />
          </td><td>
            <div class="proportion" id="percentage">

            </div>
          </td>
        </div>
      </tr>

There are many further improvements that can be made to your code, I'd start off reading about addEventListener rather than using inline event handlers like onblur

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