Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

89
Vistas
JavaScript Variables adds themselves and stacks up

So when I want to calculate variable1 + variable2 * variable3 it only add themselves and stacks up.
Code:

function randomRange(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function func() {
    var pxL = localStorage["pxL"];
    var val = document.getElementById("bp").innerHTML;
    document.getElementById("bp").innerHTML = funcCalc(val, randomRange(1, 10), pxL);
}
function funcCalc(val, random, px) {
    return val + random * px;
}

The thing is that in HTML document there is element which indicates the variable, and the variable would add itself + random and multiply by third variable on button click. The problem is that the variables calculate themselves and instead of changing the innerHTML, they adds it to innerHTML.
HTML:

<stat id="bpT">BPT: <textarea class="statIndic" id="bp" disabled>0</textarea></stat>

I want output like 15 and not 015 etc.

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to get an integer, not a string:

    var pxL = parseFloat(localStorage["pxL"]);
    var val = parseFloat(document.getElementById("bp").innerHTML);
    document.getElementById("bp").innerHTML = funcCalc(val, randomRange(1, 10), pxL);

.innerHTML returns a string for sure. Depending on how you store the variable, localStorage might return a string or number.


You can also use Number():

    var pxL = Number(localStorage["pxL"]);
    var val = Number(document.getElementById("bp").innerHTML);
7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.