Freecodecamp: Understand String Immutability - I don't understand what it wants me to do

Created on 1 Jan 2016  路  27Comments  路  Source: freeCodeCamp/freeCodeCamp

These are the only given instructions:
Instructions
Correct the assignment to myStr to achieve the desired effect.

The code given is this:

// Setup
var myStr = "Jello World";

// Only change code below this line

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

And the error states:

Attempted to assign to readonly property.

This might just be me, but I don't really know what it's asking for, as there are no missions/goals under the written instructions that must be finished in order to pass onto the next class. :/

help wanted

Most helpful comment

Hi...
just comment out
//myStr[0] = "H";
And type
myStr = "Hello World"

All 27 comments

I think this part in the instructions:

Note that this does not mean that myStr cannot be changed, just that the individual characters of a string literal cannot be changed.

is what this waypoint is trying to teach. String immutability = you can't change individual letters in a string by selecting them with [].

In order to do that you would have to change the string into an array (["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]), change the desired letter with myStr[index], and then change the array back into a string to be displayed properly.

// Setup
var myStr = "Jello World";

Its rather simple when you think about it or say it out loud..
  • You have a string called Jello world...
  • You can turn it into an array.....
  • You can change it by its index
  • Then you can turn the array back into a string

Yeah, when I looked back at it, I realized how simple the solution was. Thanks for the help!

This was a weird exercise because they didn't post the tests that needed to pass.

For anyone that's stuck,

  1. Turn "Jello World" into an array => ["J","e","l","l","o","","W","o","r","l","d"]
  2. myStr[0] = "H"
  3. Turn new array back into string and remove the commas
  4. Final result should say "Hello World"

Spoiler - solution posted below!

// Only change code below this line
myStr = ["J","e","l","l","o"," ","W","o","r","l","d"];
myStr[0] = "H";
myStr = myStr.toString().replace(/,/g,"");

Thanks for the help! it's works. I just wanna add a explication for this line of code.

myStr.toString().replace(/ ,/g," ");

Here the var myStr is transform [ by the method .toString() ] from array to string and the method replace() search a space in the array and make it a string " " ! .

This is jus a explication for mi friends in Colombia that dont understand that line of code.
Sorry for my english.

It was a great explanation process! thanks guys!
Please, let's try to have these conversations at gitter (the chat at https://gitter.im/FreeCodeCamp/HelpJavaScript), though. We have a few channels meant specifically for this purpose.
Github's issues are meant for bugs and enhancement suggestions/contributions, etc.

Hi...
just comment out
//myStr[0] = "H";
And type
myStr = "Hello World"

@maosan132 thanks for your help, but as I said in my previous comment, we do not want the issue system to become a coding help centre, as we already have Gitter channels in place precisely for that purpose.

It may belong on gitter, but all I ever see there is all I don't need when I'm seeking, Google brought me here and it solved my issue, AND, with some thought I can understand what's been done

im having trouble with this challenge too, given the learned skills thus far in the curriculum i cannot figure it out; where can i speak to somebody about this?

I've gotten this far without remembering that "Gitter" is the Chat function on FreeCodeCamp.com in case it helps anyone. Also, the expected answer can't be using an array etc. since that hasn't been taught yet & FCC is great at making sure we have what we need...even if this was harder than it should have been for how easy it actually is. See maosan132 above.

This challenge is hard to understand..some of the solutions here are good, but for example
myStr = myStr.toString().replace(/,/g,""); hasn麓t been covered so far (the replace part), so it麓s not a good solution, in my opinion...

@freeCodeCamp/moderators This topic has re-awaken. Could we be falling into a cognitive bias, assuming it's more understandable than it really is?

@hallaathrad quite possible. I think the "vague instructions" and confusion might be coming from the last sentence

Correct the assignment to myStr to achieve the desired effect that is intended by myStr[0] = "H" ;.

The "intended" effect can be interpreted in many ways. This could be changed to something like

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

That does seem quite more legible. However, I'll defer that judgment to others.
Let's hear you, people.

It's been a few months since I did this one but I know it was frustrating since I ended up here to figure it out and to try and help others.

@erictleung is right,"Correct the assignment to myStr so it contains the string value of Hello World using the method shown in the example above." would be a big help.

However when a person is this new even the words "assignment", "string value" and "method" require extra thinking because those are coding related words that I looked for to help guide me to the correct solution. In this case I think the word "method" added to my confusion because I knew that is a coding related way to change things. Combine that with the super complicated (to a beginner) examples I found at the start of these comments and I was overwhelmed.

So I'd suggest using ericleung's sentence but changing "the method" to "an approach" so that it reads, "Correct the assignment to myStr so it contains the string value of 'Hello World' using an approach shown in the example above." This keeps me from seeking out a JavaScript Object Methods when the desired approach is WAY easier.

If even more clarity is preferred then I suggest the following sentence, "Correct the assignment to myStr so it contains the string value of 'Hello World' using the _correct approach_ shown in the example above." Since I think I was also confused by having the initial "myStr[0] = "H";" in there despite it clearly stating "Fix me" next to it, this version might be better.

Finally, the most clear option I could think of was "Make myString equal to 'Hello World' using the second approach shown above." However this is likely less desirable because it fails to incorporate the vocabulary I mentioned above the learning of which is an important part. Besides, this statement practically writes the answer for you.

@DaveGus thanks for your feedback. And agreed, "method" is a very loaded word in programming. I don't think we should say change the text using the "correct approach," because as others have discussed above, there are many different ways to "correct" the text.

So I'm for using this text

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

@erictleung I agree. Now can you teach me how to not ramble on so much? :-)

@DaveGus no worries! I like to think of it as extensive feedback :smile: But we appreciate it. It lets us know what you're thinking about and what kind of suggestions others are putting out there. Thanks for sharing your thoughts on the matter! I'll tag this as Help Wanted to get changed.

[Hey guys, I thing that it's saying that if once created a strings it can not be altered again!
only you can change variable except which value you have assigned :)

I'M AMAZED I came here because I was Stuck! I read all this advice that I could barely grasp by SMART, well meaning folks. I now believe they are all mistaken, because as it turns out, this is a "trick question" assignment!!! I just read the error message and asked "why is it trying to do that?" Because the code on line 7 told it to, that's why!! SPOILER ALERT!!!! All ya do is: Step 1. DELETE the code on Line 7. Step 2 Re-initialize your myStr variable to = "Hello World". Then click "Run tests (ctrl + enter)" and the big green ball of success bounces on your screen!! Grumpy cat uses the litter box instead of pooping in your slippers.

@Bennpierce4077 Hi! I'm glad you figured it out. That is indeed the intended solution. We're clarifying the instructions to hopefully make it more obvious.

very tricky but i got it..

cheers, i completed the challange.
first: you have to delete the line
myStr[0] = "H"; // Fix Me
second: add simply:
removed solution by mod
the result is:
Boom-shakalaka!
:-)

removed solution by mod

// Setup
var myStr = "Jello World";

// Only change code below this line

removed solution by mod

Locking this since it's already closed. Please do not post solutions. You are welcome to help in the chat room on Gitter.

Was this page helpful?
0 / 5 - 0 ratings