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>
dhilt
  • 18,707
  • 8
  • 70
  • 85
gonzalo2000
  • 628
  • 1
  • 8
  • 22
  • Have you tried to step through that code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – litelite Nov 08 '17 at 19:10
  • Also, read the doc about [replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) – litelite Nov 08 '17 at 19:11

1 Answers1

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 ", "???");
dhilt
  • 18,707
  • 8
  • 70
  • 85