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

187
Views
Las variables de JavaScript se agregan y se acumulan

Entonces, cuando quiero calcular variable1 + variable2 * variable3 , solo se suman y se acumulan.
Código:

 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; }

El caso es que en el documento HTML hay un elemento que indica la variable, y la variable se agregaría + al azar y se multiplicaría por la tercera variable al hacer clic en el botón. El problema es que las variables se calculan solas y en vez de cambiar el innerHTML, lo añaden a innerHTML.
HTML:

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

Quiero una salida como 15 y no 015 , etc.

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

0

Necesita obtener un número entero, no una cadena:

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

.innerHTML devuelve una cadena con seguridad. Dependiendo de cómo almacene la variable, localStorage podría devolver una cadena o un número.


También puedes usar Number() :

 var pxL = Number(localStorage["pxL"]); var val = Number(document.getElementById("bp").innerHTML);
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!