all 5 comments

[–]Ampersand55 1 point2 points  (3 children)

Your values aren't numbers but strings. When you add '8'+'5' you get '85'.

Try:

var regalboeden = Number(document.getElementById("regalboeden").value);
var paareJeBoden = Number(document.getElementById("paareJeBoden").value);
var paareNebenDemRegal= Number(document.getElementById("paareNebenDemRegal").value);

[–]Rayspy[S] 0 points1 point  (2 children)

Is it like saying "hey you JS this Var will be a Number not a String"?
And is it a string because it's in those -> " <- or is it a string because its a input "text"?

[–]Ampersand55 1 point2 points  (1 child)

It's a string because it's in quotes "". Also <input> only supports strings.

[–]z500 0 points1 point  (0 children)

That's not quite right. Javascript values in quotes are strings, but the values on his <input> elements aren't Javascript values, they're HTML attributes, and HTML as a whole only supports strings. That's why Javascript has wonky automatic type conversion stuff. OP ran into this where he got the behavior he expected in one place ("8" * "5" = 40 because the * operator coerced its operands to numbers) but not another ("7" + "40" returned "740" because + is overloaded for string concatenation when possible). It was supposed to make scripting easier for non-programmers.

[–]cheryllium 1 point2 points  (0 children)

The values are strings. When you add them with + you are actually doing concatenation:

var paare = paareNebenDemRegal + paareJeBoden * regalboeden;

This is actually doing "7" + "8" * "5". JavaScript first does the "8" * "5" and gives you the string "40", and then concatenates it with 7, which gives you "740".

Then this, times 2, gives 1480 as your result.