Results 1 to 4 of 4

Thread: Get variable onClick

  1. #1
    Join Date
    Jun 2014

    Question Get variable onClick

    I'm trying to create a skate dice app but i can't seem to get the variable onclick of the button

    HTML Code:
    <input id="roll" type="button"  value="Roll" />
    function init(){
    var die = Math.random();
    if(die <= 0.01) {
    } else if (die <= 0.02) {
    } else if (die <= 0.03) {
    } else if (die <= 0.04) {
        alert("Fakie Ollie")
    } else if (die <= 0.05) {
    } else if (die <= 0.06) {
       alert("Fakie 180")
    } else if (die <= 0.07) {
    } else if (die <= 0.08) {
    } else if (die <= 0.09) {
        alert("Varial Kickflip")
    } else if (die <= 0.10) {
        alert("Varial Heelflip")
    } else if (die <= 0.11) {

  2. #2
    Join Date
    Jul 2013
    Hi Provision,

    Firstly, welcome to the forum!

    The reason your init() function was not working was due to the lack of the roll() function you called. Use the developers tab on any main browser by pressing f12 or going into the browser tools. From here you can look at the javascript console for the page and it will tell you of any errors it can see (which usually is all syntactical errors and naturally is cannot find logical errors).

    With this you see that there is a problem with line 2 "object not found" ... the roll() object. (though I have a suspicion you just left this function out of the post :P)

    Moving on, Math.random() gives another between 1 and 0. This can be anything from 0.99999 to 0.1111 (you get the jist) so your if statement was only picking up a few of these numbers causing the function to only show an alert roughly once every 10 times (mathematically speaking). Please see the code below:

    <button id='roll'>Click</button>
    function init(){
        //Do loop to perform the action once and repeat if we dont get a number from 0-11//
    	  var die = (Math.random()*100).toFixed(0);
    	while(die >11); // Higher than 11, boo that's not what we want. try again //
        var elm = document.getElementById('roll').onclick=function(){roll(die);};
    function roll(die) //Now that roll is actually a function :P //
    var answers = [                     // Due to the simplistic need of the if statment an array works just fine // 
    				'Fakie Ollie',
    				'Fakie 180',
    				'Varial Kickflip',
    				'Varial Heelflip',
    			                     // Remember an array is javascript goes 0,1,2,3,4,5 ext //
    alert(answers[die-1]); /* Give me my answer! kk so die = 6 so i want answer[6] right?
                              No, answer[6] = Kickflip. We want answer[5] ... o, kk minus 1 then. */
    Now hopefully the commenting will answer most of the questions that might be going through your head but just to explain how I get the number and my logic behind it.

    You want a number between 0 and 11 from my understanding but if we were to just go var die = (Math.random()*100).toFixed(0); (with toFixed making it so there is no decimal place) we would get a number between 0 and 100. Not good. But if we just multiply is by 10 then its only going to give us 0-10 ... um ... we need 11. The easiest way i see doing this is like I have done.

    This was a rushed answer due to needing to grab some sleep so i hope its not too terrible English or under explained - just reply if you get confused.

  3. #3
    Join Date
    Nov 2010
    no need for the while loop, etc. This will give you a "random" number between (and including 1 and 11):
    var die = Math.floor(Math.random() * 11) + 1;

  4. #4
    Quote Originally Posted by xelawho View Post
    no need for the while loop, etc. This will give you a "random" number between (and including 1 and 11):
    var die = Math.floor(Math.random() * 11) + 1;
    If you are going to retrieve value from array, you need random values from 0 - 10

    var die = Math.floor(Math.random() * 11) ;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles