Not happy with solution

Hello everyone,

Not sure how to blur out the code as I do know it contains a solution

I have now been learning to code for a month, I seem to be getting better at answering and working out solutions. However I am not happy with my solutions as I feel they are too verbose and contain too many variables, which I could see being a problem once I start working with larger blocks of code.

See the below code for an example and is there any advice on how I could make my code more readable and less verbose with not so many variables, or is this an acceptable solution?

This is my first ever post and I have seen how brilliant the community is here so I would like to get involved and hopefully gain a better understanding to become a stronger coder in the future.

Your code so far

function myReplace(str, before, after) {
let words = str.split(" ")
let index = words.indexOf(before)
let word = words[index]
let regex = /^[A-Z]/
if (regex.test(word)) {
let refinedWord = after[0].toUpperCase() + after.slice(1)
let result = str.replace(before, refinedWord)
return result

} else {
let lowerCaps = after[0].toLowerCase() + after.slice(1)
return str.replace(before, lowerCaps) 



(myReplace("His name is Tom", "Tom", "john"))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.

Challenge: Search and Replace

Link to the challenge:

I would take a look at the possible solutions provided in the hints for this challenge. The important thing is that you got a working solution. Looking at the other other solutions will give you some ideas about how you can refactor your code and possibly get rid of some unnecessary noise or expose you to new ways of solving a problem.

But keep in mind, other than working correctly, the most important thing about code is that it is readable by humans (IMHO). Compare solution 2 to solution 5 in the hints. Which one would you want to read for the first time and figure out how it works?

As far as cleaning up variables, one easy way to do that is to look where you are defining a variable and only use it once. For example:

let regex = /^[A-Z]/
if (regex.test(word)) {

Most people would just use:

if (/^[A-Z]/.test(word)) {

Same with your return statement, change

let result = str.replace(before, refinedWord)
return result


return str.replace(before, refinedWord);

Also, I know it’s trendy not to but I would highly recommend you put semicolons at the end of every statement. It may come back to bite you one day.

1 Like

Hey @InexpCoder!

You can use [spoiler] and [/spoiler] tags on the line above and below your solution code. That is how I edited your original post.

I definitely see your point about how much more readable solution 2 is in the hints compared to solution 5. The variable examples clean up was exactly what I was looking for and I’ll look to apply this to future challenges.

I will admit I have got lazy with semicolons but I will start getting into the habit of using them again! Thanks for the brilliant reply.

1 Like

Thank you @jwilkins.oboe!
I will make sure I remember for next time.

When I did that challenge my solution was really bulky and had a lot of unnecessary repetition and variable declarations. But the first time around it is totally fine if your solution is a little messy because you can always go back later and refactor it.

As far as semicolons, in higher level languages like JavaScript, you can get away with forgetting a semicolon even though it is good practice to have them. If you start working with lower level languages like C then your code won’t even compile without the semicolons. Over time you will remember to place the semicolons in the right places but JavaScript won’t yell at you like C will. :sweat_smile:

1 Like

Thanks for the reply, it’s reassuring to know that other people have had similar issues with messy code. The intermediate problems are already so challenging for me without the issue of refactoring so coming back to it later, like you said, is a good idea.

I wasn’t aware of the importance of semicolons with other languages like C so that is good to know, and I will make sure I start getting into the habit of doing it all the time so it becomes second nature! :slight_smile:

1 Like