Is there a way for a beginner to follow to come up with clean code solution?

Hi everyone,
I am fairly a beginner and want to learn to become a good programmer, I
tried to solve this chalange “Intermediate Algorithm Scripting: Diff Two Arrays” with filter() method in order to avoid using double for loops I tried couple of times I couldn’t so I looked at the solution I found this one:

  function diffArray(arr1, arr2) {
      return arr1
        .concat(arr2)
        .filter(
            item => !arr1.includes(item) || !arr2.includes(item)
        )
    }

    diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

I became very frusterated because with my way of thinking It will be very hard to me to come up with this kind of solution, I am aware of the answer “Keep practicing” this is what I am doing right now but with my thinking process I am not improving, I keep writing code and debugging with trial and error, this is the only way I can solve programming challenges.
While there are uncountable resources out there to teach you programming language syntax and basic programming concepts I couldn’t find decent resources for learning efficient programming techniques.
I want to hear your thoughts about the subject.
Thank you in advance.

You need to put the [spoiler] tags outside the backticks if you want to blur a solution

But, yes, practice is the only way. Try solving the algorithms with pseudocode, break it down to the smallest steps possible, and then when you can explain it to someone else, start translating it to the coding language of your choice. If you find you don’t know a method to do what your pseudocode says, google it, it will exist almost surely, so don’t worry about that.

2 Likes

Besides just practicing, remember that you have an amazing resource in your fellow campers. You can come here to discuss your in-progress solution. We will do our best to ask you questions or give you small nudges so that you can solve the problem yourself.

1 Like

Well, I think that “just keep practicing” is hogwash when when it comes to algorithms. You need computer science fundamentals. IMHO, whoever wrote that solution also does, because:

  1. As Don Knuth would say, we should write code for legibility first.
  2. Boolean logic is a key part of any intro to comp sci class. Being forced to prove that (NOT X) OR (NOT Y) is the same as NOT(x AND Y) is boring, may make you lose interest, blah, blah, blah… But it keeps you from losing interest when you can’t understand things like this. Use that Boolean identity and see whether the “reworded” version makes immediate sense to you.

Knuth’s point (1) about legibility is part of why functional programming is so powerful. You actually LOSE performance by not implementing things with raw loops, but you GAIN programmer productivity in tasks like writing/refactoring/reading code, and from a business standpoint, CPU cycles are often cheaper than programmer time.

I applaud your desire to “do things both ways.” I make myself do it all the time to stay sharp. This becomes really helpful when the edge-cases near the ends of arrays make reading lower-order functions more annoying than reading a bunch of nested for loops with if/else statements in the mix.

If algorithms are messing you up, you do need more practice. But, blind practice isn’t going to get you there. Designed practice, meant to make you do more than learn syntax, is required. I know that CS50 doesn’t do a lot of javascript (Python, I think), but their syllabus looks meaty in terms of the pain points of neophyte programmers who complain about getting lost at the exact same point in the curriculum as you.

If chaining functions is messing you up, more JS practice may really be all you need, with one caveat. Draw this stuff out on some graph paper. Picture the arrays and visualize what each higher-order function is doing.

1 Like

Thank you all for your inputs, my deep regards to vipatron because you described exactly what I am suffering although my original question was about how to improve my code and use the assets provided by javascripts methods and design patterns.
Well if we believe as a fact that there is nothing beside practicing help you learn programming effectively, I would dare to proclaim that all courses, books, university curriculum or even mentors have no pedagogical value whatsoever in teaching programming .
And for the solution it is from “Intermediate Code Solution” template provided by FCC in the hint section.
Finally I wish everyone involved in writing programming tutorials follow Donald Knuth
advice to the letter, our lives will be much easier.

Okay, but practically speaking:

if (the boolean logic identity I mentioned is not perfectly obvious){
  read up on boolean identities;
  do the practice problems;
}
if (you don't understand what happened with the chained functions){
  if (you aren't 100%-bet-your-mother's-life-sure about what object "filter" is being called on){
    read up on inputs/outputs(return values) of functions
  }
  practice;
}

The reason it’s hard is because this is where FCC falls down. In CSS & HTML, it’s great. Task.introduce().showExample().testLearner(); is the basic idea. For any education that can be described as “here’s how to do this one thing”, it’s great. For any education where concepts are required, it isn’t. How FCC strives to make up for this is by introducing people to CS50 and by means of the forum. The big problem is that Javascript isn’t a great way to teach people these concepts. It can be done, but not without mentioning how C deals with arrays and what a pointer is. And CS50 focuses on Python, I believe. It’s a useful language to learn because it “makes hard things easy,” and is thus a great language for beginners.

Back to your fundamental question of “how do I get better?” “Better” means three different things:

  1. runtime speed.
    • Based on raw CPU cycles (useful for server environments), and why for loops are “better” than higher-order function calls.
  2. legibility
    • What Knuth was talking about, and why functional programming is “better” than a for loop.
  3. Elegance - a “je ne sais quoi” that combines the former two with a succinctness of code, usually a way to express an algorithm that is highly performant.
    • This requires some math. DS & A, and (some of) Discrete Math, usually, to get enough “tools in your toolkit.”
    • It’s about matching the difficulty of the answer to the scope of the problem. You wouldn’t implement Dijkstra’s algorithm to find something that could be accomplished by serial search on a 10-element array.

Now, I know that Data Structures & algorithms scare people, but if you look at what people say they encountered in their job searches, knowing about them is usually required for any real programming, instead of just plugging pieces together. That being said, many respectable jobs don’t require tons of specialized DS&A, they just require stacks, queues, and CRUD.

So, if you’re evaluating a resource, ask those who used it whether they felt they learned core programming concepts. Keep learning them THEN practicing them, and you will get where you want to go.

“Keep Practicing” == paying your dues in time to gain experience, working with it for months/years/decades… continuous learning via books, ref manuals, videos… reading a lot… studying good code example… reading other people’s code… lots and lots of debugging… learning from your mistakes.

It’s like learning how to play an instrument. Same advice of “keep practicing.”

1 Like

Speaking of “elegance…”