Home > Net >  best way to replace all male pronouns with female ones JavaScript
best way to replace all male pronouns with female ones JavaScript

Time:11-17

I made a JavaScript quote generator but wanted to add something to it. So I decided to figure out how to swap male verbiage for female. The API is an array with 1.5k quotes as objects. As a new coder how would you approach this problem? full project is here https://keller-johnson.github.io/quote-generator/

//Show new quote
function newQuote() {
  loading();
  //Pick a random from apiQuotes array
  const quote = apiQuotes[Math.floor(Math.random() * apiQuotes.length)];

  //Searching for male terms and making them female

  //turn quote.text into an array this will make it easier to match the male terms and make them 
  //because if we tried to do .replace or .replaceAll it would replace parts of substrings as well
  let quoteArray = quote.text.split(" ");
  // now we are making a if statement to turn these generic terms into women specific terms
  let newArrayQuote = quoteArray.map((index) => {
    if (index === "he") {
      return "she";
    }
    if (index === "he.") {
      return "she.";
    }
    if (index === "He") {
      return "She";
    }
    if (index === "his") {
      return "hers";
    }
    if (index === "His") {
      return "Hers";
    }
    if (index === "him") {
      return "her";
    }
    if (index === "him.") {
      return "her.";
    }
    if (index === "Him") {
      return "Her";
    }
    if (index === "men") {
      return "women";
    }
    if (index === "men,") {
      return "women,";
    }
    if (index === "Men") {
      return "Women";
    }
    if (index === "man") {
      return "woman";
    }
    if (index === "man.") {
      return "woman.";
    }
    if (index === "Man") {
      return "Woman";
    }
    if (index === "himself") {
      return "herself";
    }
    if (index === "himself.") {
      return "herself.";
    } else {
      return index;
    }
  });
  //turning the newArrayQuote back into string
  let stringQuote = newArrayQuote.join(" ");

  //Check if Author field is blank and replace it with unknown
  if (!quote.author) {
    authorText.textContent = "Unknown";
  } else {
    authorText.textContent = quote.author;
  }
  // Check quote length to determine the styling
  if (stringQuote.length > 120) {
    quoteText.classList.add("long-quote");
  } else {
    quoteText.classList.remove("long-quote");
  }

  //Set Quote, Hide Loader
  complete();

  quoteText.textContent = stringQuote;
}

I have a working model but it's not very DRY and it's killing me because I know there has to be a better way.

CodePudding user response:

You can use an object for mapping:

const mapping = {
  'he': 'she',
  ...
  'himself.': 'herself.'
}

and then in your code use simply object access:

let newArrayQuote = quoteArray.map((word) => {
   const transformed = mapping[word];
   return transformed
     ? transformed
     : word
});

Apart from that I don't know how DRY you want to go, since I don't know which parts of he function will be used outside of this use-case.

CodePudding user response:

you can change the code in a better way

you can create an associative array for storing data, and also you don't need to keep both ("his", "His") instead of that you can find out the ASCII value of the first letter and if the value is between 65-90 then change the first letter with upper case.

Maybe it will help you. if you want to code then tell me i will provide that too

  • Related