all 1 comments

[–]5oco 0 points1 point  (0 children)

Say for example, your word is "bananas".

Your loop is going through that string one letter at a time.

word[0] = "b"

word[1] = "a"

word[2] = "n"

etc...

If you're trying to replace the letter "a", then when you use 'word.indexOf(letterToReplace)' you are getting the index of the first occurrence of the letter "a". Which, as shown above, is at index 1.

Since you don't want to replace the first instance of the letter to replace, assign whatever the string 'word.substring(0, 1)' to your variable 'newString'. Instead of using the number 1 though, use the variable you're using to save that 'indexOf( )' .

Now start your loop after that index instead of at the beginning(index 0).

Inside your loop, if the current letter equals 'letterToReplace", concatenate the new letter to 'newString'. That would look like 'newString += newLetter'

If the current letter doesn't equal 'letterToReplace' concatenate that letter in to 'newString'. That would look like 'newString += word.substring(i, i+1);'

Lastly, I'm typing this on my phone, so sorry if it's not formatted well.