I am creating a script to populate an email template with random variables, using the replace() method.
Unfortunately, I am struggling to find a way of inserting a gender pronoun, based on the gender of a random name chosen from a string. Is there a way to do this using the excerpts of the scripts below (or am I missing something incredibly obvious)?
HTML
<p id="lineTwo">firstName told genderPronounOne friends...</p>
JavaScript
//Random Name
var maleNames = ['Tom', 'Tony'];
var femaleNames = ['Tina', 'Tracy'];
var maleRandom = maleNames[Math.round(Math.random()*(maleNames.length-1))]+'\n';
var femaleRandom = femaleNames[Math.round(Math.random()*(femaleNames.length-1))]+'\n';
var nameGender = [maleRandom, femaleRandom
var name = nameGender[Math.round(Math.random()*(nameGender.length-1))]+'\n';
//Determine Gender
var pronounOne;
if(/* this is where I'm struggling */){
pronounOne = 'his';
} else {
pronounOne = 'her';
}
//Replace HTML
var replaceHTML = document.getElementById("lineTwo").innerHTML;
var replaceName = replaceHTML.replace("firstName", name);
var replacePronoun = replaceHTML.replace("genderPronounOne", pronounOne);
document.getElementById("lineTwo").innerHTML = replaceName;
document.getElementById("lineTwo").innerHTML = recplacePronoun;
Can anyone help? Thank you.
答案 0 :(得分:2)
Store the 0 or 1 in a variable and use it
var mf = Math.round(Math.random()*(nameGender.length-1))
var name = nameGender[mf]+'\n';
//Determine Gender
var pronounOne = mf ? "her" :'his';
答案 1 :(得分:0)
How about you check to see if the name that you selected is in the array of male names?
if (maleNames.indexOf(name) > -1) {
pronounOne = 'his';
} else {
pronounOne = 'her';
}