I’m making a simple Fahrenheit to Celcius number conversion function with a button and two text fields as an exercise and I want to make sure my function will stop when someone puts a value in each text field and then tries to execute the function with onclick?
Here is part of the Javascript everything else is ok:
function transfer ()
{
var fahr= document.form.fahrenheit.value;
var celc= document.form.celcius.value;
if ( isNaN(celc) || isNaN(fahr))
{
alert('Please provide a valid number');
return;
}
else if (//what do I put here? == true)
}
alert('conversion will not calculate correctly with values in both fields');
return;
}
I’m really new to programming I hope there is enough info here.
First you (try to) check whether either field has a valid value. This can be done by checking whether both fields are Not a Number (NaN). To check both condifitions use
&&(boolean and operator):Next, you seem to want to check whether both fields are filled in (so you do not know direction the conversion needs to be). So check if one field is empty, this can be done by checking of both fields are set (you already know that there is at least one valid value due to the previous check):