There is absolutely no point in declaring the variable name
twice. You do it once inside generator()
and once as a global which is not a very good idea.
Instead, what you want is to execute the function generator()
once you click on the button. This function needs to return the name
.
To rewrite this:
function generator(){
var firstName = ["John","Mike","Robert","Patrick"],
lastName = ["Smith","Johnson","Williams","Anderson"],
randomNumber1 = parseInt(Math.random() * firstName.length, 10),
randomNumber2 = parseInt(Math.random() * lastName.length, 10);
return firstName[randomNumber1] + " " + lastName[randomNumber2];
}
$(function(){
$(".nameCreationButton").click(function(){
//var name = generator();
$(".displayName").append(generator());
});
});