Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
How to print an initial value using document write

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>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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");

about 4 years ago · Juan Pablo Isaza Report

0

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>
about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!