-1

I am trying to generate a string of random characters just like this example here

$(document).ready(function(){

  $('#randomize').click(function() {

    var text = "";
    var possible = "michaeljordanisthebestbasketballplayerofalltime";


    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;

    console.log(text)
    $('.shuffle_text').html(text);

  });

});

for some reason it is not working on click - the console.log for text is still empty when I run it. Any ideas?

http://jsfiddle.net/#&togetherjs=QVf3hGm8la

Community
  • 1
  • 1
Joe Isaacson
  • 4,012
  • 6
  • 30
  • 40

1 Answers1

0
$(document).ready(function(){

  $('#randomize').click(function() {

    var text = "";
    var possible = "michaeljordanisthebestbasketballplayerofalltime";


    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    console.log(text)
    $('.shuffle_text').html(text);

  });

});

Removed return.

@jsfiddle, you don't need to return anything because jQuery won't do anything with it.

Also, replace your possible string to something like "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789". This will increase the randomness since you have much the same characters in the current string.

Wouter0100
  • 430
  • 6
  • 19