0

In my html I have the following function call

<input value="Roll" onclick="roll()" type="button">

which should call the function roll() that is defined as follows before:

    <head>
    <script language="javascript">
    // roll function
    // roll button pressed to roll the die and update as needed
    function roll(){
        wintotal = document.JForm.totalpoints.value;    
        var validate = Validate();
        var p1total = document.JForm.p1turn.value;
        var p2total = document.JForm.p2turn.value;

        var dienumber = Math.floor(Math.random() * (6 - 1 +1)) + 1;

        if (validate){  
            // put together image for die graphic that was rolled
            document.getElementById("dice").innerHTML = '<img src="die_face_'+dienumber+'.png"/>';


        }
        else if (validate == 0){
            document.getElementById("message").innerHTML ="ERROR: Play to points not in range";
        }
        else{
            document.getElementById("message").innerHTML ="ERROR: Play to points not in valid integer";
        }
    }  // end roll function
    </script>
</head><body>

however when i click the button the HTML page I get an error saying roll is undefined even though it is clearly defined above it, anyone have any ideas?

2
  • Are you sure that is roll the problem and not the other functions like Validate or another? Commented Sep 28, 2013 at 1:19
  • Could you post the exact error message you're getting? Commented Sep 28, 2013 at 1:35

1 Answer 1

1

I tried that code and it's like you said. Then I deleted that Validate thing and now it works normal. What do you need it for and why? It's not that clear on the example posted!

I'd have written this as a comment, but I haven't got enough reputation... :(

EDIT: Like I said in the comment, if the problem is just checking if the value is an integer or not, you can implement this function.

function isInt(n)
{
    return n % 1 === 0;
} 
Sign up to request clarification or add additional context in comments.

2 Comments

You are correct, i am using the validate function to return a value based on if a text box's value is an integer or not. function Validate(){ var total = Number(document.JForm.totalpoints.value); var good = 1; var range = 0; var type = 3; if (Math.floor(total) == total){ if ((total >= 50) && (total <= 100)){ return good; } else{ reutrn range; } } else{ return type; } }
You could implement function isInt(n) {return n % 1 === 0;} to check whether your input is an integer or not!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.