+ 1
Please help fix my c.oninput for k.value
window.onload = function(){ var c = document.getElementById("celsius"); var f = document.getElementById("fahrenheit"); var k = document.getElementById("kelvin") c.oninput = function(){ f.value = (c.value*(9/4)) + 32; k.value = c.value + 273;// this isn't printing out the expected result } f.oninput = function(){ c.value = (f.value-32)*(5/9); k.value = ((f.value-32)*(5/9))+273; } k.oninput = function(){ c.value = (k.value-273); f.value = ((k.value-273)*(9/5))+32; } }
19 Answers
+ 2
Chimobi Ekwunife ,
I improved most of your code edit it to what you see fit.
https://code.sololearn.com/WWDvOlSaCOxP
You should use
parseFloat()
to turn the values into floats so that it can be converted properly.
+ 1
in Html
<input type="text" id="celsius" value="">
+ 1
Chimobi Ekwunife , I improved your code, I think you'll find it useful
https://code.sololearn.com/WWDvOlSaCOxP
+ 1
Seems to be working on my part Chimobi Ekwunife
I change value of either one input box, and the other two updates their values. Not sure where the problem is ...
+ 1
Ipang when u change that of celsius, the result is meant to be celsius value+273
+ 1
For if I input 2 on the celsius scale, I suppose I should get 2+273=275.
But I get 2273 instead. Ipang
+ 1
Chimobi Ekwunife
The reason for that was because c.value is a string until we convert it to a numeric type. SoloProg's code had anticipated the issue by converting c.value (a string) into a numeric type (float)
Here's what happened ...
c.value = "2"
When evaluating `c.value + 273` JS treat the number 273 as a string because it has c.value "2" to the left, thus string concatenation took place instead of actual arithmetic addition.
When we explicitly convert c.value to a numeric type however, arithmetic addition will take place because both operands are numeric.
Hope that helps clear some doubt ...
+ 1
What problem did you face can you send the error message
0
Thanks soloprog, but it didn't work
0
Please attach a code bit link in post Descriprion instead, it's hard to check things up when we can only see the JS code ...
https://www.sololearn.com/post/75089/?ref=app
0
SoloProg there is an error in your code
0
I just did a quick fix copy the code and edit it to your liking
0
SoloProg there is still an error in line 9 tho
0
And nothing is happening when I insert a number
0
SoloProg am sorry for stressing u this much, but nothing is still happening, no conversion is made, and there are still errors in line 8 and 11
0
I mean, there is an error in your console and when insert a number nothing happens SoloProg
0
It worked for me perfectly fine ask another sololearner