My code calculates correctly except I can not get the initial temperature to display, in this case "45.9", to print.
Current output:
function FtoC(Fahrenheit) { return (Fahrenheit - 32) * 5 / 9; } F = 7.722222222222222 C
Where FtoC(Fahrenheit) { return (Fahrenheit - 32) * 5 / 9; } is to equal the number 45.9.
Desired output:
45.9 F = 7.722222222222222 C
Code:
<script>
function FtoC(Fahrenheit)
{
return (Fahrenheit - 32) * 5 / 9;
}
var theFahrenheit = FtoC(45.9);
document.write("<br>");
document.write(FtoC + " F = " + theFahrenheit + " C");
</script>
you can try returning an array from your function FtoC like below:
function FtoC(Fahrenheit)
{
return [Fahrenheit, (Fahrenheit - 32) * 5 / 9];
}
var theFahrenheit = FtoC(45.9);
document.write("<br>");
document.write(theFahrenheit[0] + " F = " + theFahrenheit[1] + " C");
or Else if you already know the input, you can directly use it instead of returning it in an array.
document.write(45.9 + " F = " + theFahrenheit[1] + " C");
Try this:
<script>
function FtoC(Fahrenheit) {
return (Fahrenheit - 32) * 5 / 9;
}
let temperature = 45.9;
var theFahrenheit = FtoC(temperature);
document.write("<br>");
document.write(temperature + " F = " + theFahrenheit + " C");
</script>
On this line:
document.write(FtoC + " F = " + theFahrenheit + " C");
You're writing out FtoC, which is your function, not the temperature. Try introducing a new variable to hold the initial temperature, and write that out to the document instead.
<script>
function FtoC(Fahrenheit)
{
return (Fahrenheit - 32) * 5 / 9;
}
var theTemp = 45.9
var theFahrenheit = FtoC(theTemp);
document.write("<br>");
document.write(theTemp + " F = " + theFahrenheit + " C");
</script>
I also noticed in that line that you're writing theFahrenheit + " C" which seems backwards. Since the variable theFahrenheit is holding the temperature after conversion, it should be named something else.