Having a problem with code

I am having a problem with the code I’m posting below. I keep getting NaN instead of a numerical value, and I can’t determine the problem.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Kerf Height</title>
        <link rel="stylesheet" href="example3.css"> 
    </head>
    <body>
        <p>Stepover Kerf Height</p>
        <form id="stepOver" name="stepOver">
            <label for="ballNoseDiameter">Tool Diameter</label>
            <input type="number" id="ballNoseDiameter" placeholder=".312" step=".0001"/>
            <label for="stepOverAmount">Step Over</label>
            <input type="number" id="stepOverAmount" placeholder=".281" step=".0001"/>
            <label for="kerfHeight">Kerf Height</label>
            <input type="text" id="kerfHeight" readonly/>
            <input type="submit"/>
        </form>
        <script>
            var findKerfHeight = function(event) {
                    event.preventDefault();
                var ballNoseDiameter = parseInt(document.getElementById('ballNoseDiameter').value);
                var stepOverAmount = parseInt(document.getElementById('stepOverAmount').value);
                //
                var ballNoseRadius = (ballNoseDiameter/2);
                //
                var kerfHeight2 = ballNoseRadius - (Math.sqrt(ballNoseRadius * ballNoseRadius) -(stepOver/2 * stepOver/2));
                //
                document.getElementById('kerfHeight').value = Math.round(kerfHeight2 * 10000) / 10000;
            }
                //
            var form = document.getElementById('stepOver');
            form.addEventListener('submit', findKerfHeight, false);
        </script>
    </body>
</html>

Hi @enigmania, the problem is the stepOver variable which refers to the form with the ID “stepOver” – kind of a shorthand for document.getElementById('stepOver'), which should better be avoided for various reasons – and you can’t divide or multiple a form element, hence you get NaN.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.