Learning html and javascript so I went to building this calculator.

I'm not sure what my issue is but I feel it's something with the startcalc() function calling the checkarray().

Please take a look at my code on jsfiddle and you will see my two functions at the bottom of the html.

http://jsfiddle.net/kane/EwpgU/

Recommended Answers

I'm not sure what my issue is but I feel it's something with the startcalc() function calling the checkarray().

I don't know either.

The reason is very simple you didn't really explain what is wrong or what error you are getting from the code.

I don't like guessing …

Jump to Post

All 4 Replies

Member Avatar

I'm not sure what my issue is but I feel it's something with the startcalc() function calling the checkarray().

I don't know either.

The reason is very simple you didn't really explain what is wrong or what error you are getting from the code.

I don't like guessing so if you expect someone to help you then you need to explain what is wrong with the code or what error you are getting.

Plus you should post your code on the thread.

You're right. Sorry about that. My function checkarray() works works fine and returns the correct values. However when I try to use it from the startcalc() function the alert box prints "undefined". Any idea of what I should look for. Also I'd like to embed all code into the html page so that the user can use this calculator offline so calling them rom an external file isn't what I want to do, either.

Thank you,
Kane

            <form id="contact-form" action="/" method="post">
                <div>
                     <h3>PT Calculator</h3>

                    <label>  <span>Age: (required)</span>

                        <input placeholder="Enter age" name="agename"
                        type="text" tabindex="1" required autofocus>
                    </label>
                </div>
                <div>
                    <label>
                        <label>  <span>Weight: (required)</span>

                            <input placeholder="Enter weight" name="weightname"
                            type="text" tabindex="1" required>
                        </label>
                </div>
                <div>
                    <label> <span>Pushups: (required)</span>

                        <input placeholder="Enter pushups" name="pushupname"
                        type="text" tabindex="2" required>
                    </label>
                </div>
                <div>
                    <label>  <span>Situps: (required)</span>

                        <input placeholder="Enter situps" name="situpname"
                        type="text" tabindex="3" required>
                    </label>
                </div>
                <div>
                    <label>  <span>Run: (required)</span>

                        <input placeholder="Enter time" name="runname"
                        type="text" tabindex="4" required>
                    </label>
                </div>
                <div>
                    <label>  <span>if bike:</span>

                        <input placeholder="Enter time" name="bikename" type="text"
                        tabindex="5">
                    </label>
                </div>
                <div>
                    <label>  <span>if elliptical:</span>

                        <input placeholder="Enter time" name="ellipticalname"
                        tabindex="6" type="text"></input>
                    </label>
                </div>
                <div>
                    <button name="submit" onclick="startcalc()" type="submit" id="contact-submit">Calculate Score</button>
                </div>
            </form>
            <!-- /Form -->
        </div>
    </div>
</body>
<script>
    function startcalc() {
        var age = document.getElementsByName('agename')[0].value;
        var weight = document.getElementsByName('weightname')[0].value;
        var pushup = document.getElementsByName('pushupname')[0].value;
        var situp = document.getElementsByName('situpname')[0].value;
        var run = document.getElementsByName('runname')[0].value;
        if (age < 19) {
            var score = [45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100];
            var situparray = [50, 54, 59, 62, 71, 81, 90, 93, 98, 102, 107, 109];
            var pushuparray = [42, 46, 49, 51, 60, 68, 76, 79, 82, 86, 91, 92];
            var scoredsitup = checkarray(situp, situparray);
            var scoredpushup = checkarray(pushup, pushuparray);
            alert(scoredsitup)
            alert(scoredpushup)
        }
    }

    function checkarray(score, array) {
        var len = array.length;
        if (score < array[0]) {
            return (0);
        }
        if (score > array[len - 1]) {
            return (performance[len - 1]);
        }
        for (var i = 0; i < len; i++) {
            if (score >= array[i] && score < array[i + 1]) {
                return (performance[i]);
            }
        }
    }
</script>

</html>
Member Avatar

However when I try to use it from the startcalc() function the alert box prints "undefined".

Regarding about undefined. There are couple ways to solve this

1) check spelling to match with your startcalc() function.
2) check if all function is define.

So far ...

You don't have these in your startcalc() function:

ellipticalname

bikename

Add it then I think that will remove the undefined unless you have more issue that you didn't mention

Turns out the checkarray() did not know what the performance variable was since that was declared in startcalc() and never passed... Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.