-7

So when I type:

function x(z) {
    if (z !== 'y') return ("There is an error!")

    z = z.replace(/y/g, 'Canada');

    return x(z);
}

x('y')

I receive make "There is an error". But I am trying to get it to say 'Canada'.

What's wrong with my code?

Thanks!

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • Just replace `return x(z);` by `return z;` – Magicprog.fr Apr 04 '16 at 13:54
  • I'm not convinced this is worthy of 5 downvotes? Could the voters explain? It's basic but not badly explained. – Liam Apr 04 '16 at 13:55
  • 1
    Possible duplicate of [Replacing all occurrences of a string in JavaScript](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) – Ben Fransen Apr 04 '16 at 13:57
  • @Liam I think they downvoted because this question is not clear enough, don't mind them! As long this user get good answers and your question is solved :)\ – node_modules Apr 04 '16 at 13:57
  • It's not my question... @C0dekid.php :) – Liam Apr 04 '16 at 13:58
  • Oh I see @Liam, I have edited my comment, sorry! haha :) – node_modules Apr 04 '16 at 13:59
  • @AndyLe It is down voted because it is not clearly explained. The main reason is because "replacing a string with a string" can be done like this: `myStr = "new stuff"`. That replaces the value of the string myStr with another string, which is word for word what you asked for. So there is clearly something you want to do besides just replacing a string with a string. Explain what you really want to accomplish. Another reason is because you have an if statement in the first line of your function that is not explained, very odd, and has no obvious reason for being there. – Matt C Apr 04 '16 at 15:18

5 Answers5

7

You are unnecessarily making a recursive call there,

function x(z) {
  if (z !== 'y') return ("There is an error!")
  z = z.replace(/y/g, 'Canada');
  return z;
}

That is not required at all in your context.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

I think this is what you want to do.

function x(z) {
    if (z !== 'y') return ("There is an error!")

    z = z.replace(/y/g, 'Canada');

    return z;
}

alert(x('y')); // Canada
Akshay Khandelwal
  • 1,570
  • 11
  • 19
0

Remove unnecessary call to x(z). See below

function x(z) {
    if (z !== 'y') return ("There is an error!");

    z = z.replace(/y/g, 'Canada');

    return z; // changed from return x(z), unnecessary recursion
}

console.log(x('y'));
Samundra
  • 1,869
  • 17
  • 28
0

Because z is already replaced with Canada at the second execution of the recursion. BTW, there is no point to use regex in your case.

function x(z) {
    if (z !== 'y') return ("There is an error!")
    z = z.replace('y', 'Canada');
}
x('y')
Lewis
  • 14,132
  • 12
  • 66
  • 87
0

That code is doing exactly what you are telling it to.

You're saying: return "There is an error!" if var z is not equal to 'y', otherwise call the same function with a value not equal to 'y'.

Then you're call the function with 'y'.

dkinzer
  • 32,179
  • 12
  • 66
  • 85