Grow your CSS skills. Land your dream job.

Get random word from prompt imput

  • # March 2, 2013 at 7:59 am

    I’m having a tough time doing this.

    And what I mean is, pull a prompt box, and if you input either “Test1″ Test2″ “Test 3″, then there’s a random chance that when you click okay, an alert box says either test1, test2, test3 randomly.

    Any idea?

    # March 2, 2013 at 9:24 am

    You could switch the text to be outputted using a `switch` statement with a random number generation as the condition to switch:

    var numRand = Math.floor(Math.random()*2) // generate number between 1 and 3

    switch(numRand) {

    case 0:
    // code for 1st scenario
    break;
    case 1:
    // code for 2nd scenario
    break;
    case 2:
    // code for 3rd scenario
    break;
    default:
    // what to do if, for some reason, an invalid number was parsed
    break;

    }

    # March 2, 2013 at 10:54 am

    @tomrogers123
    You random number calculation is wrong.

    // This will never give you 2. Only 0, 1
    Math.floor(Math.random()*2);

    // This will give you 0-2 outcomes
    Math.round(Math.random()*2);
    Math.floor(Math.random()*3);

    # March 2, 2013 at 5:17 pm

    Don’t use `Math.round(Math.random()*2)`, unless you want the chance to get 1 to be twice as much as the chance to get 0 or 2, as in 25% 50% 25% chance.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".