Search code examples
javascripthtmlfunctionprompt

How to assign a prompt value to function parameter


So i have this code:

<div class="wrapper">
    <p id="ex"></p>
    <script>
        function celMessager() {
            var celMs = window.prompt("Enter the celsius grades:");
            celMs = parseFloat(celMs);
                if (celMs == null) {
                document.getElementById("ex").innerHTML = "Error.";
            }
            else {
                document.getElementById("ex").innerHTML = celConversor();
            }

        }
    </script>

    <button class="button" type="button" onclick="
     celMessager();
    ">
     Conversor Celsius.
    </button>
</div>

And i want to store the value of the user input (celMs) into the only parameter that celConversor() takes:

function celConversor(celGrades) {
     fahGrades = (celGrades * 9/5) + 32;
     return fahGrades;
}

So the number that the user enters, become the celsius grades that are going to change into fahrenheit and show it as a paragraph. How can I do this? tried making the celGrades variable = to celMs and celMs to celGrades, but it always returns "NaN" in the paragraph, even after i added the "parseFloat" line.


Solution

  • You're getting this error celConversor requires a parameter, but you're not passing any parameters to it. Change this line:

    document.getElementById("ex").innerHTML = celConversor();
    

    to this:

    document.getElementById("ex").innerHTML = celConversor(celMs);