0

Hi. I am new to this site so I hope you will all be able to help me with my problem. I am just starting to learn JavaScript so any help/feedback would be greatly appreciated.

I have to make a form that if you put a temperature in a textbox (either Celsius or Fahrenheit) and you click on a button (To Celsius or To Fahrenheit) it will convert it to the corresponding temperature which will appear in another textbox.

I have gotten this far but am getting a NaN value in the coverted temperature

I have been trying to work this out for over a week so any help would be really appreciated.

My code is:

</form>

<!-- used to calculate the temperature conversion from Celcius to Farenheit and Farenheit to Celcius
!-->
	<script>

		function celcius()
		{
			document.getElementById("answers").value = parseFloat(document.getElementById("input").value) + Math.round(100/(212-32) * (this.value - 32 ));
		}

		function farenheit()
		{
			document.getElementById("answers").value = parseFloat(document.getElementById("input").value) + Math.round((212-32)/100 * this.value + 32);
		}
	</script>
<h1>Temperature Conversion Tool</h1>
<h3>Enter a value into the first box below, then click on what it is to be converted to
Temperature</h3>
<form name="temperature">
&nbsp;<input id="input" value="0" type="text"> 
&nbsp;<input id="answers" value="0" type="text">
<br>
<input value="To Celcius" onClick="celcius() " type="button">
<input value="To Farenheit" onClick="farenheit()
" type="button">
<input name="reset" value="Reset" type="reset"><br>
</form>

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
4
Replies
7
Views
7 Years
Discussion Span
Last Post by LizzyJo
0

Change the script to this :

<script>
function celcius()
{
var inp = parseFloat(document.getElementById("input").value);
document.getElementById("answers").value = inp + Math.round(100/(212-32) * (inp - 32 ));
}

function farenheit()
{
var inp = parseFloat(document.getElementById("input").value);
document.getElementById("answers").value = inp + Math.round((212-32)/100 * (inp + 32));
}
</script>
0

Change the script to this :

<script>
function celcius()
{
var inp = parseFloat(document.getElementById("input").value);
document.getElementById("answers").value = inp + Math.round(100/(212-32) * (inp - 32 ));
}

function farenheit()
{
var inp = parseFloat(document.getElementById("input").value);
document.getElementById("answers").value = inp + Math.round((212-32)/100 * (inp + 32));
}
</script>

Thank you so much for the help. It works beautifully. No more NaN error.
I think I may have the wrong formula now as I get the wrong converted numbers. 86 farenheit = 30 celcius but my conversion comes up with 116.

0

i think it is ....

function celcius()
{
var inp = parseFloat(document.getElementById("input").value);
document.getElementById("answers").value = 5/9 * (inp - 32) ;
}

function farenheit()
{
var inp = parseFloat(document.getElementById("input").value);
document.getElementById("answers").value = Math.round(9/5 * inp) + 32;
}
0

Yes that's it. Thank you. Thank you. Thank you!

Thank you so much vasanth19 for all your help. One problem solved and now onto the next one.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.