+ 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; } }

26th Nov 2022, 7:07 AM
Chimobi Ekwunife
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.
26th Nov 2022, 7:46 PM
SoloProg
SoloProg - avatar
+ 1
in Html <input type="text" id="celsius" value="">
26th Nov 2022, 7:51 AM
SoloProg
SoloProg - avatar
+ 1
Chimobi Ekwunife , I improved your code, I think you'll find it useful https://code.sololearn.com/WWDvOlSaCOxP
26th Nov 2022, 11:02 AM
SoloProg
SoloProg - avatar
26th Nov 2022, 2:55 PM
Chimobi Ekwunife
+ 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 ...
26th Nov 2022, 3:05 PM
Ipang
+ 1
Ipang when u change that of celsius, the result is meant to be celsius value+273
26th Nov 2022, 3:15 PM
Chimobi Ekwunife
+ 1
For if I input 2 on the celsius scale, I suppose I should get 2+273=275. But I get 2273 instead. Ipang
26th Nov 2022, 3:17 PM
Chimobi Ekwunife
+ 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 ...
26th Nov 2022, 3:55 PM
Ipang
+ 1
SoloProg Ipang Thanks a lot
26th Nov 2022, 4:52 PM
Chimobi Ekwunife
+ 1
What problem did you face can you send the error message
26th Nov 2022, 8:09 PM
SoloProg
SoloProg - avatar
0
Thanks soloprog, but it didn't work
26th Nov 2022, 9:06 AM
Chimobi Ekwunife
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
26th Nov 2022, 9:59 AM
Ipang
0
SoloProg there is an error in your code
26th Nov 2022, 2:56 PM
Chimobi Ekwunife
0
I just did a quick fix copy the code and edit it to your liking
26th Nov 2022, 3:36 PM
SoloProg
SoloProg - avatar
0
SoloProg there is still an error in line 9 tho
26th Nov 2022, 4:53 PM
Chimobi Ekwunife
0
And nothing is happening when I insert a number
26th Nov 2022, 6:02 PM
Chimobi Ekwunife
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
26th Nov 2022, 7:52 PM
Chimobi Ekwunife
0
I mean, there is an error in your console and when insert a number nothing happens SoloProg
26th Nov 2022, 8:15 PM
Chimobi Ekwunife
0
It worked for me perfectly fine ask another sololearner
26th Nov 2022, 8:39 PM
SoloProg
SoloProg - avatar