0

I have an array of Spanish sentences, all of which include one instance of "por" or "para" (capitalized or not). I'm trying to load one random sentence on the browser, replacing the test case of por/para for question marks ("???"). Thus far it's loading the original sentence without applying the character operation.

function setSentence() {
  // Return random int between min (included) and max (excluded)
  function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
  }

  const sentenceList = [
    "El mundo era tan reciente, que muchas cosas carecían de nombre, y para mencionarlas había que señalarías con el dedo.", "Melquíades, que era un hombre honrado, le previno: «Para eso no sirve.»", "Úrsula Iguarán, su mujer, que contaba con aquellos animales para ensanchar el desmedrado patrimonio doméstico, no consiguió disuadirlo.",
  ];

  var sentence = sentenceList[getRandomInt(0, sentenceList.length)];

  var $sentence = $("#test-sentence");

  var testSentence = sentence;

  if (testSentence.includes("por ") || testSentence.includes("Por ")) {
    if (testSentence.includes("por ")) {
      testSentence.replace("por ", "???");
    } else {
      testSentence.replace("Por ", "???");
    }
  } else {
    if (testSentence.includes("para ")) {
      testSentence.replace("para ", "???");
    } else {
      testSentence.replace("Para ", "???");
    }
  }
  $sentence.text(testSentence);
}
setSentence();

And the html:

<div class="container">  
  <p><span id="test-sentence"></span></p>    
</div>
2
  • Have you tried to step through that code with a debugger? Commented Nov 8, 2017 at 19:10
  • Also, read the doc about replace Commented Nov 8, 2017 at 19:11

1 Answer 1

1

Per MDN:

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement.

So you need to do an assignment each time you want to modify your string with .replace():

testSentence = testSentence.replace("para ", "???");
Sign up to request clarification or add additional context in comments.

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.