Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading