0

im trying to make some Algorithm function with javascript and get some problems

function Algorithm() {
    var endone;
    var endtwo;
    var endtheend;
    var v1 = document.getElementsByName("v1")[0].value; //worth to 91
    var v2 = document.getElementsByName("v2")[0].value; //worth to 61
    var v3 = document.getElementsByName("v3")[0].value; //worth to 20

    endone = Math.round(v1 * 0.30);

    endtwo = Math.round(((v2 + v3) / 2) * 0.70);

    endtheend = endone + endtwo;


    document.getElementById("ending").innerHTML = "end : " + endtheend;
}

if im doing the same Algorithm with a calculator im getting 55.65 , but when im trying to use this function somehow im getting 2169.

someone might know what is the problem and show me how to solve her?

anar chiff
  • 13
  • 8

3 Answers3

2

The problem is that v1, v2 and v3 are not numbers. They are strings. So each calculation you make is relies on implicit conversions and operations between strings.

For instance, in the following snippete we have an implicit conversion of the the string value "91" to a double floating number and then the usual mulitplication is done.

var v1 = "91";
console.log(v1*0.3);

On the other hand below:

var v2 = "61";
var v3 = "20";

console.log((v2 + v3) / 2)

We have a string concatenation "61"+"20" results in a new string "6120" and then "6120" is implicitly converted to a double floating number and the division with 2 is done.

What's the solution ?

You have to parse these values either by using parseInt or parseFloat, like below:

var v2 = "61";
var v3 = "20";

console.log((parseInt(v2,10) + parseInt(v3,10)) / 2)
Christos
  • 53,228
  • 8
  • 76
  • 108
0

When you get an HTMLInputElement's value property, what you get is a string. And the + operator applied to two strings merely concatenates them, so if for instance v2 == "7" and v3 === "0", when you do (v2 + v3), you'll get "70".

The solution to your problem is to simply pass the values through parseInt:

var v1 = parseInt(document.getElementsByName("v1")[0].value, 10);
var v2 = parseInt(document.getElementsByName("v2")[0].value, 10);
var v3 = parseInt(document.getElementsByName("v3")[0].value, 10);
// The second argument to parseInt isn't needed if you only target newer browsers.

I'd suggest you read up on type coercion in JavaScript for more info.

lleaff
  • 4,249
  • 17
  • 23
0

The issue is all of your values (v1, v2, v3) are String type. You need to convert them into Number first. So the following code should work :

function Algorithm() {
    var endone;
    var endtwo;
    var endtheend;
    var v1 = Number(document.getElementsByName("v1")[0].value);
    var v2 = Number(document.getElementsByName("v2")[0].value);
    var v3 = Number(document.getElementsByName("v3")[0].value);
    endone = Math.round(v1 * 0.30);
    endtwo = Math.round(((v2 + v3) / 2) * 0.70);
    endtheend = endone + endtwo;
    document.getElementById("ending").innerHTML = "end : " + endtheend;
}

you can also use parseInt if your value contains any alphabetic characters.

m87
  • 4,445
  • 3
  • 16
  • 31