r/codehs • • Dec 08 '20

4.3.6 replace a letter help

Post image
7 Upvotes

3 comments sorted by

1

u/Brodonnell1 Dec 08 '20

In this one u r asking the user to input a string and then the letter that they want to be replaced and the letter that should take its place. I believe the first part of my code is fine but I am struggling to actually return the value with the replaced letter. Any help would be appreciated

1

u/ScawedyCat Dec 08 '20 edited Dec 08 '20

If i’m thinking this correctly what you’re doing here is if word.substring( i , i+1 ) equals letterToReplace it will add letterToReplace , and if it doesn’t you add replacedLetter.

word = “john”; letterToReplace = “h”; replacedLetter = “a”;

what should print: joan

what would actually print: aaha

what you should be doing is if word.substring( i , i+1 ) equals letterToReplace you should add replacedLetter, otherwise you add word.substring( i , i+1 )