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
ἔρως
ἔρως4mo ago
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 it
lanszelot
lanszelotOP4mo ago
I do not understand this . Always return a value, and .... these I do not understand
ἔρως
ἔρως4mo ago
i fixed a typo there
lanszelot
lanszelotOP4mo ago
So intensity cannot devided with 100 because intensity is a string, and give back intensity itself. But why not error?
ἔρως
ἔρως4mo ago
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 code
lanszelot
lanszelotOP4mo ago
If 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
ἔρως
ἔρως4mo ago
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
lanszelot
lanszelotOP4mo ago
I see
ἔρως
ἔρως4mo ago
nonetheless, it is still a bug in your code
lanszelot
lanszelotOP4mo ago
Thank you so much for your help. What bug?
ἔρως
ἔρως4mo ago
well, you were trying to do math with a string that's a bug on your part
lanszelot
lanszelotOP4mo ago
it was, but your solution solve it. Where else did I count with string? Or you mean before you solve it?
ἔρως
ἔρως4mo ago
before you fixed it

Did you find this page helpful?