JavaScript not counting correctly
Hello,
In this code : https://codepen.io/lanszelot/pen/NPPJKoG
Inside the for loop workoutHrKarvonen variables counting wrong.
Line 42 : intensity/100 this is not counting somehow.
intensity not devided with 100 , and it count with intensity itself.
Why?
13 Replies
that is because
pulse
is a string
when you do math then + a string, it concatenates the values together and you get a string
use inputs with type number, and then use .valueAsNumber
workoutHrKarvonen = (maxHrSimple-pulse)*(intensity/100)+pulse;
<-- this line will ALWAYS return the value and then the resting heart rate together, as a string, if you don't fix itI do not understand this . Always return a value, and .... these I do not understand
i fixed a typo there
So intensity cannot devided with 100 because intensity is a string, and give back intensity itself. But why not error?
no
that is not whatbis hapoening
happening
what happens to the left of the
+
doesnt matter much
what is on the right side, the pulse
variable, is a string
if you do anything + string
you get a string
if you do 123 + "456"
you get "123456"
that is what is happening to your codeIf you check it.
Age: 50 Weight:100 Rest HR: 52
Max HR: 170 , intensity: 50
workoutHrKarvonen = (170-52)*(50)+52 = 5952
So not devided with 100, everything else working
Your soultion is correct, but I do not understand why not drop me error.
Why it is counting with string
I set back the original and you could see the error is not intensity is devided with 100
because there is no error
that is a very well defined operation in the spec, and it's behaviour is expected
it wasnt expected to you, but it is the correct behaviour
I see
nonetheless, it is still a bug in your code
Thank you so much for your help.
What bug?
well, you were trying to do math with a string
that's a bug on your part
it was, but your solution solve it. Where else did I count with string?
Or you mean before you solve it?
before you fixed it