Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

document.write not printing

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>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading