I am beginner who thought before doing the String Methods section of the course "You know what, let's test my skills". And boy my skills were tested. After I completed the challenge my jaw dropped, with the solution.
Had/Have this happened to y'all. Where you make something complicated and found out that there was a simple solution?
Solution
![](https://lemmy.ml/pictrs/image/e67483d5-ebd0-4007-93fa-830b53a5f981.png)
My Code
![](https://lemmy.ml/pictrs/image/f05b7c85-894d-4b27-898e-2486bf31553c.png)
Oh yeah all the time. Its what taught me to RTFM because in higher languages like JS or Python there typically already is a built-in function to manipulate basic types like arrays and strings, so my goal is usually to exhaust the API reference for a certain object and google around before committing to writing my own for-loop to iterate over an array.
But props on your code! It is very legible which is always the best place to start at before optimizing. Write legible code and when you're sure there are no more features you need to add then start optimizing.
Problem with RTFM for me is I always forget what I had read the next day, how do you remember what you read?
That's the neat part, you don't! Don't have to remember shit, just look it up again.
I look up the exact behavior of some basic Kotlin collection lambdas all the time. Does filter return elements with true or false lambda return value? I'll just Google it instead of relying on my memory.
a couple notes
- you should declare all variables with
let
before you assign them, it's good practice and you can enforce it by enabling strict mode - putat the beginning of your function (or the entire script). Of course it's only needed in browsers, strict mode is usually enabled by default in most tools. ;
- try not to execute extra code if you can help it. For example, in this case only the final
reversedWord
value matters, so you can do it at the end as opposed to on every iteration. Your code right now works in O(N^2) - with every new character in the string its speed decreases exponentially, but it should work in O(N) - a linear time. If you couldn't createreversedWord
at the end, you could still initialize it with an empty string and append some text with+=
on every iteration, that still works in O(N) time as you don't have to recreate the entire string on every iteration.
O(N^2) - with every new character in the string its speed decreases exponentially,
Try to be precise when teaching others. O(N^2) increases "quadratically" not exponentially. O(k^N) would be exponentially increasing with N.
(For point 1) Got it, from now on variables will be declared with
let
. I don't understand what ismaybe you can explain it. ;
(For point 2) I was testing to see if
reversedWord
printed the desired output in the Console, forgot to remove it after finishing the program. I also don't understand what 'O(N^2)' and 'O(N)' is, but+=
-ing an empty string is a great idea, why didn't i think of that.
- you should declare all variables with