I'm doing intermidiate algorithm problem at freecodecamp, I don't know if I should learn advanced solving method

I'm doing intermidiate algorithm problem at freecodecamp, I don't know if I should learn advanced solving method
0

#1

I find a website has some method and explanations, I just feel all those simplest methods it’s a bit hard for me, but I wanna good at that algorithm.

e.g
Pig Latin
link: https://www.freecodecamp.org/challenges/pig-latin
simple way:
function translate(str)
var vowelIndex = /[aeiou]/.exec(str).index;
return str.slice(vowelIndex) + (str.substr(0, vowelIndex) || “w”) + “ay”;
}

hard way ES5:
function translate(str) {
return str.replace(/(\w*?)([aeiou])(\w*)/, function (_, before, vowel, after) {
return vowel + after + before || “w” + ay;
})
}

Diffrent form ES6:
function translate(str) {
return str.replace(/(\w*?)([aeiou])(\w*)/, (_, before, vowel, after) => ${vowel}${after}${before || "w"}ay);
}

If I choose to understand those hard way, it will take me a lot of time. So I wanna improve those basic first, But I don’t know If I’m right or not?
At the same time, I wanna spend more time to figure those problems by myself, but it just too hard for me. The weird thing was I can saw other people do always spend a lot of time for those seems, they alway can found some special way by themselves, Should I do that?


#2

you picked up new pattern or methods one by one along the way.
there is always more than one way to solve a problem… so it would be difficult to find every way to solve a problem.
is good to find different way to do it… but no need to get stressed out about it.
everyone learned bit by bit … and eventually knows a language better.
so take your time, learn new pattern/method when you feel insterested. and just save em to read in the future if it’s too difficult to understand.
at least that’s what i would do :smiley:


#3

Thank for your advice. :smile:


#4

Understanding regex (that’s what is used in last two solutions) and being able to write a complex regex is not an easy skill to master.

For example, this is regex to validate email address:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

I think even seasoned professional will require a bit of time to understand what it does.

My advice is don’t spend too much time diving into regex as a beginner.
Watch this video, it’ll give you regex basics that will be enough for most cases: