freeCodeCamp Challenge Guide: Understand String Immutability

Understand String Immutability


Hints

Hint 1

Instead of Jello World, myStr should be assigned Hello World.


Solutions

Solution 1 (Click to Show/Hide)
// Setup
var myStr = "Jello World";
// Only change code below this line
myStr = "Hello World";

Code Explanation

String literals such as "Jello World" cannot be changed by the individual letter (hence being immutable), so the variable containing the incorrect string must be replaced with the desired string using the assignment operator =

4 Likes

I am still confused on how exactly to do this

// Setup
var myStr = “Jello World”;

// Only change code below this line

var myStr = “Hello World”; // Fix Me

2 Likes

I keep getting an TypeError: 0 is read only message when I tried that. Still not sure if it’s cause I’m missing something or its an error on their side.

6 Likes

any can fix this problem ? I still have this case

I ran into the TypeError as well when I copies “ello World” and pasted it next to the H. However, when I cleared my changes and manually typed it out it worked fine. I hope this helps.

2 Likes

Nothing above worked for me, it’s strange. So I just did like this:

// Setup
var myStr = “Jello World”;
// Only change code below this line
myStr =“Hello World”;
myStr[0];

Weird :sweat_smile:, I know (it also had a red underline) but it worked. :point_up_2:
So my question is: How can I improve it?

5 Likes
// Setup
var myStr = "Jello World";

// Only change code below this line

myStr[0] = "H"; // Fix Me

/////////////////////////////////////////////////////////////////////////////////

// Setup
var myStr = "Jello World";

// Only change code below this line

myStr = "Hello World"; // Fix Me
4 Likes

The instructions are hard to understand. You need to change the bottom code to correct the mistake from “Jello World” to “Hello World”

2 Likes

// Setup
var myStr = “Jello World”;

// Only change code below this line
myStr = ‘Hello World’;
myStr[0] ;

1 Like

Just delete = “H” in the end of the code and left only myStr[0];

3 Likes

that works but i think it’s confusing ppl bc the instructions read only change code below …
change code below line to…

var myStr = “Hello World”;
** myStr[0];**

i used a console.log (myStr[0]);

I got: var myStr = “Hello World”;