0

I have searched long and far for a way to have a function change a given global variable using the function's arguments and local variables inside as the "shortcut" to it. Because I can't find the right words to explain this well, I will be adding 2 scripts bellow, the first being what it looks like and the second being what it should act like.

Note: The examples given are meant to replace all matching characters/text of a location to a new value.

Example 1:

function ReplaceAll(loc,old,new_text) {
    while (loc.includes(old)) {
        var new1 = loc.replace(old,new_text);
    }
    loc = new1;
}

This script [Example 1] is how I wish to set up my script yet it does not work because it only catches the value of the location and not setting the original value when changed.

Example 2:

var example = "What is? Where is? Why is? Who is?";
ReplaceAll(example,"is","was");
console.log(example);

Preferred Output: "What was? Where was? Why was? Who was?"

This script [Example 2] is the effect that I am attempting to achieve to be able to make this and other scripts of mine work.

PS: I am not trying to ask for people to do the work for me thus going against the rules of Stack Overflow, I just want to know, if possible, how this can be done.

Thank you.

  • This isn't possible. Parameters are passed by value, not by reference. – Barmar Feb 24 '18 at 00:10
  • BTW, your function will get into an infinite loop if `new_text` contains `old`, e.g. `replaceAll(string, example, "is", "is not")`. – Barmar Feb 24 '18 at 00:11
  • Not possible, why dont you want to just return the new value? – Klaimmore Feb 24 '18 at 00:11
  • @Barmar Thank you for bringing that to my attention, I will just add an if statement to fix that. – The Real Fake Admin Feb 24 '18 at 00:15
  • Why not just use a regexp? `loc.replace(/old/g, 'new')` – Barmar Feb 24 '18 at 00:16
  • @Barmar I am not using that because I am yet to learn specifically what you can and can't do with those types of parameters (the ones using "/", "g", etc.) – The Real Fake Admin Feb 24 '18 at 00:23
  • But what would be the correct way to do this without variables, I still need this for other functions with similar issues? – The Real Fake Admin Feb 24 '18 at 00:35
  • [Regular expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) are used in JavaScript , with a global flag ( the "g" thing), to replace all occurrences of a something in a string using the [`string.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) method. [W3Schools](https://www.w3schools.com/jsref/jsref_replace.asp) may be of help. – traktor Feb 24 '18 at 01:05
  • I am already learning JS (and more) through [W3Schools](https://www.w3schools.com/) but I have not had a chance to learn that yet. Thank you. – The Real Fake Admin Feb 24 '18 at 01:14
  • You can also read a tutorial on regular expressions at regular-expression.info – Barmar Feb 24 '18 at 05:53

0 Answers0