1

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.

1
  • 1
    You have a typo here var nameGender = [maleRandom, femaleRandom Commented Jul 31, 2015 at 19:44

2 Answers 2

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';
Sign up to request clarification or add additional context in comments.

Comments

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';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.