Need help with: Understand String Immutability

Correct the assignment to myStr so it contains the string value of Hello World using the approach shown in the example above.
// Setup

var myStr = “Jello World”;

// Only change code below this line

myStr[0] = “H”; // Fix Me

Here is my code but it wasn’t accepted:

myStr = “Hello World”; // Fix Me

no string methods change the string they operate on, they all return new strings .
they cannot change, we can only ever make new strings
so that’s why string are immutable

Have you removed the faulty statement in the code? Otherwise it wouldn’t work

Try running this:

// Setup
var myStr = "Jello World";
// Only change code below this line
myStr[0] = "H";
console.log('myStr: ' + myStr);
// Has myStr changed from "Jello World"?

You just need to modify the string provided by removing the zero from the index bracket and also remove the fix the issue comment from the line and re write it as follows

myStr = “Hello World”;

I could pass after modifying the given code to the above code. Cheers.

1 Like