When you write code its good to have some plans and steps to do but you need to put them in right place. Your step 4 is totally useless cause it is OUTSIDE of the result function so when you call the function there are some missing parts.
The replace method returns a NEW string so when you use this method, you should save it in a variable unlike yours.
I mentioned this method a lot in past replies as well but it seems like you didn’t read the documentation of it. I put a link down below, i hope you study it.
By the way, there are 2 :insertX: in the text, you better use replaceAll method for that. Also I recommend to read about variable hoisting. I found an error for that in your code as well.
I post a link down below that is my code when I was studying like you I wrote it. You might wanna check it out: