Home > other >  document.write not printing
document.write not printing

Time:04-06

When I enter the inputs, it only outputs the value of Celsius but not Fahrenheit. I want my input to have 1 decimal place. Why is that and how do I fix it?

<script>
            
            let fahrenheit = prompt("Input a Fahrenheit temperature:");
            
            let celsius = (fahrenheit - 32) * 5 / 9;
            
        </script>
        <p>
            <script>document.write("Fahrenheit: "   fahrenheit.toFixed(1));</script>   
        </p>
        <p>
            <script>
                document.write("Celsius: "   celsius.toFixed(1));
            </script>  
        </p>

CodePudding user response:

toFixed() is a method of Number. You first need to convert your string to a float

<script>
            
            let fahrenheit = prompt("Input a Fahrenheit temperature:");
            
            let celsius = (fahrenheit - 32) * 5 / 9;
            
        </script>
        <p>
            <script>document.write("Fahrenheit: "   parseFloat(fahrenheit).toFixed(1));</script>   
        </p>
        <p>
            <script>
                document.write("Celsius: "   parseFloat(celsius).toFixed(1));
            </script>  
        </p>

  • Related