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

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. 😕

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 27 (10 by maintainers)

Most upvoted comments

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

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

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,”");

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

removed solution by mod

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.

@DaveGus no worries! I like to think of it as extensive feedback 😄 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.

@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.

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.

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

@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.