-3

I'm currently using Visual Studio to build my website and I'm getting stuck on my username generator. I need it because I don't want any inappropriate words and this is a children's website so I don't want to use real information. I thought about using an array of strings and then adding them together but for some reason, I can't get it to work -__- any help would be appreciated. I'm in coding boot camp and my teacher said this would be a useful place for information. Thanks.

So far, this is what I have:

var a = ["Small", "Blue", "Ugly"];
var b = ["Bear", "Dog", "Banana"];

var rA = Math.floor(Math.random()*a.length);
var rB = Math.floor(Math.random()*b.length);
var name = a[rA] + b[rB];
alert(name);

Now my issue is I'm trying to write a function that will generate a new name with every click. I know I need function myFunction() and an onclick= event. Hopefully this is not a vague as before.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
  • So is the question how to generate a random string at all, or how to exclude certain inappropriate words that might come up randomly? Presumably you'd also need to exclude any user names that have already been taken... – nnnnnn May 10 '15 at 12:45
  • I'm just worrying about how to generate the random string for now. I'll focus on the excluding certain words later @nnnnnn...if you have any ideas, please do share. – Jasmine Gheith May 10 '15 at 12:47
  • Well, an array of permitted characters, then [generate a random number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Using_Math.random()) between 0 and the end of the array to select one such character. Do in a loop to build up a string of the desired length. – nnnnnn May 10 '15 at 12:49
  • also, I don't want to generate random chars, I want 2 full words to be added together. a adj and noun...(so if the array had ["Small","Blue","Ugly"]; and another had ["Bear","Dog","Banana"]; you could possibly get "BlueBanana" or "UglyBear"). – Jasmine Gheith May 10 '15 at 12:50
  • Well that'll work with something like what I just suggested. – nnnnnn May 10 '15 at 12:50
  • I'm going to try it in JSFiddle now..Thanks for the quick response :) – Jasmine Gheith May 10 '15 at 12:52

1 Answers1

6

You can try something like this, (looking at comments)

var a = ["Small", "Blue", "Ugly"];
var b = ["Bear", "Dog", "Banana"];

var rA = Math.floor(Math.random()*a.length);
var rB = Math.floor(Math.random()*b.length);
var name = a[rA] + b[rB];
Gagik
  • 96
  • 3