this post was submitted on 29 Aug 2023
17 points (94.7% liked)

General Programming Discussion

7647 readers
3 users here now

A general programming discussion community.

Rules:

  1. Be civil.
  2. Please start discussions that spark conversation

Other communities

Systems

Functional Programming

Also related

founded 5 years ago
MODERATORS
 

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

My Code

top 15 comments
sorted by: hot top controversial new old
[–] [email protected] 7 points 11 months ago (1 children)

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.

[–] [email protected] 2 points 11 months ago (3 children)

Problem with RTFM for me is I always forget what I had read the next day, how do you remember what you read?

[–] [email protected] 2 points 11 months ago

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.

[–] [email protected] 2 points 11 months ago

You have to try and remember the primitive operations available. Say you want to check if any array element matches a predicate. This is a basic operation that's abstracted for almost every language. If we look for "JS array any", we'll get the right function as an MDM result (some). And this works for almost anything. Just try to describe the operation in simple terms and google - you'll quickly find the function you need, and after some time it will become easier and easier :)

[–] [email protected] 2 points 11 months ago (1 children)

That's the neat part, you don't! Don't have to remember shit, just look it up again.

[–] [email protected] 2 points 11 months ago

Facts, you made a good point.

[–] [email protected] 6 points 11 months ago (1 children)

All the time and I've been programming professionally for 10+ years.

You'll always find a better way to do something, however, there's nothing wrong with what you initially came up with as it's easy enough to understand because you've named your variables well and your logic is easy to follow along with. I would say that is far more important than coming up with technically clever solution that may be harder for someone else to understand!

[–] [email protected] 3 points 11 months ago (1 children)

so technically clever solution < long solution with good variable names?

[–] [email protected] 4 points 11 months ago (1 children)

From a maintainability standpoint, absolutely. Computers have gotten fast enough to let programmers optimize for developer time instead

[–] [email protected] 2 points 11 months ago (1 children)

Yep, this. The code we write is eventually used or extended by other developers. Or, more commonly, yourself after not touching it for 6 to 12 months, by which point you'll have forgotten all about your clever tricks. (Speaking from experience of course 😉)

[–] [email protected] 1 points 11 months ago

Maaan, I wanna be developer soo bad but where I live they want full-stack developers, not frontend developers.

I know why they want full-stack developers, but why hire a front end developer and back end developer when you can hire full stack developer.

The closest thing to a back end development is a DevOps Engineer.

[–] [email protected] 2 points 11 months ago (2 children)

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 - put "use strict"; at 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 create reversedWord 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.
[–] [email protected] 3 points 11 months ago

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.

[–] [email protected] 1 points 11 months ago (1 children)

(For point 1) Got it, from now on variables will be declared with let. I don't understand what is "use strict"; maybe 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.

[–] [email protected] 3 points 11 months ago

"use strict"; is just a way of enabling more restrictions to make writing JS less error prone. You can find the details of what that does on MDN (or, if you don't like long verbose technical explanations, this is a pretty good summary!)