+ 1

Prompt command in Functions

Can I use a Prompt command to define the argument of a function? For example is there anything wrong in a part of code like this one? <button onclick="toCelsius()">From Fahrenheit To Celsius</button> <script> function toCelsius(Fahreneit){ return (5/9)*(Fahreneit-32); document.write("The temperature is " + toCelsius(prompt("Enter Fahrenheit value")) + "° Celsius"); } </script>

19th Oct 2018, 3:39 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar
2 Réponses
+ 1
That won't work as you can't get past the return. This will: <button onclick="toCelsius()">From Fahrenheit To Celsius</button> <script> function toCelsius() { function convert(Fahreneit){ return (5/9)*(Fahreneit-32); } document.write("The temperature is " + convert(prompt("Enter Fahrenheit value")) + "° Celsius"); }
19th Oct 2018, 4:18 PM
John Wells
John Wells - avatar
+ 1
Thanks!!
19th Oct 2018, 6:23 PM
Alessandro Palazzolo
Alessandro Palazzolo - avatar