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

245
Views
How to I sum variables as numbers?

Below you can see code I wrote to calculate an area(P) and circumfence(O) of 3 listed geometric shapes. The problem I'm encountering is when it calculates circumfence it adds strings together but I want it to add them as numbers. Any help on solving this issue is appreciated, also any parts of code you would have done different you can comment. Thanks in advance. :)

   else if (x == 3) {
                    if (p3.value.length == 0) {
                        let a2 = p1.value;
                        let b2 = p2.value;
                        let c2 = Math.sqrt(a2 * a2 + b2 * b2);
                        c2 = c2.toFixed(4);
                        let P33 = a2 * b2 / 2;
                        let O33 = a2 + b2 + c2;
                        document.getElementById("rezultat").innerHTML = "P = " + P33 + "<br> O= " + O33;
                        p3.value = c2;
                    } else if (p2.value.length == 0) {
                        let a2 = p1.value;
                        let c2 = p3.value;
                        let b2 = Math.sqrt(a2 * a2 + c2 * c2);
                        b2 = b2.toFixed(4);
                        let P32 = a2 * b2 / 2;
                        let O32 = a2 + b2 + c2;
                        document.getElementById("rezultat").innerHTML = "P = " + P32 + "<br> O= " + O32;
                        p2.value = b2;
                    } else if (p1.value.length == 0) {
                        let b2 = p2.value;
                        let c2 = p3.value;
                        let a2 = Math.sqrt(b2 * b2 + c2 * c2);
                        a2 = a2.toFixed(4);
                        let P31 = a2 * b2 / 2;
                        let O31 = a2 + b2 + c2;
                        document.getElementById("rezultat").innerHTML = "P = " + P31 + "<br> O= " + O31;
                        p1.value = a2;
                    }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use the parseInt() or Number() function. The parseInt() function parses a string argument and returns an integer. All you have to do is pass the values you are getting from your HTML element. But if you parse the decimal number, it will be rounded off to the nearest integer value. For eg :

let a2 = parseInt(p1.value);

If you want decimals as well, you can use Number

let a2 = Number(p1.value);
about 4 years ago · Juan Pablo Isaza Report

0

Use Number() to convert strings to numbers. Example:

let a2 = Number(p1.value);
let b2 = Number(p2.value);
about 4 years ago · Juan Pablo Isaza Report

0

You could use the Number() method to convert all of your string values to numbers and do your math operations with it. Having this converted only to Integers with the parseInt method is not entirely convenient in this case since you might have decimal numbers as well.

Take a look at the Number() method documentation here. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number

Cheers!

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!