-1

My values seems to be correct; however, my replace is not replacing with what I put in.

var wiseProverb = "Actions shout louder than words."; // Code will be tested 
with "You don't have to shout so loud."
var substringIndex = 0;

/* Your solution goes here */
substringIndex = wiseProverb.indexOf("shout");
wiseProverb.replace("shout", "speak");

CORRECT Testing the value of substringIndex Yours 8

INCORRECT Checking that wiseProverb was updated from "Actions shout louder than words."

Yours and expected differ. See highlights below.

Yours

Actions shout louder than words.

Expected

Actions speak louder than words.

Correct Testing the value of substringIndex with wiseProverb = "You don't have to shout so loud."

Yours

18

INCORRECT Checking that wiseProverb was updated from "You don't have to shout so loud."

Yours and expected differ. See highlights below.

Yours

You don't have to shout so loud.

Expected

You don't have to speak so loud.

Ryan
  • 189
  • 1
  • 4
  • 10
  • Possible duplicate of [Replace method doesn't work](https://stackoverflow.com/questions/1433212/replace-method-doesnt-work) – maazadeeb Feb 20 '19 at 02:43

3 Answers3

2

You need to reassign value to the variable after replace since it doesn't change original string.

wiseProverb = wiseProverb.replace("shout", "speak");
zmag
  • 7,825
  • 12
  • 32
  • 42
2

The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced.

You should reassign that value to something or just print the conversion.

var newWiseProverb = wiseProverb.replace("shout", "speak");
// this will only replace the first occurrence of that value.

// If you need to replace all occurrences you need to use a regex
var newWiseProverb = wiseProverb.replace(/shout/g, "speak");

// Or you can just  use that value in the placeholber
document.getElementById("anyplace").innerHTML = str.replace(/shout/g, "speak");

See this https://stackoverflow.com/a/54715766/2523147 for more combinations.

If you want to dig further refer https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.

prime
  • 14,464
  • 14
  • 99
  • 131
1

You didn't assign the value back to wiseProverb. By calling .replace, you created a new string with the replacement.

wiseProverb = wiseProverb.replace("shout", "speak");

mowwwalker
  • 16,634
  • 25
  • 104
  • 157