Overthinking problems

Tell us what’s happening:
Hello,

Often times I find myself overthinking practice problems and writing out unnecessarily long programs when a simple function will do exactly what I want. The below is a good example of this.

Generally I am able to find a solution through the method that I am trying to use. But when I check my answer against the FCC provided answer, many times I find I’ve done it in a different and usually less optimal way.

Is there any way to train myself start thinking more efficiently when I work through problems, or is it just something that comes with time?

Usually when I find a different answer than the one I was expecting, I will close the solution and work my way through it using the method they’ve shown. I am just worried I continually seem to be doing things the hard way.

  **Your code so far**

function filteredArray(arr, elem) {
let newArr = [];

// Only change code below this line
for (let i = 0; i < (arr.length); i++) {
  let count = 0;
  for (let j = 0; j < arr[i].length; j++) {
    if (arr[i][j] == elem) {
      count += 1;
    } else if (count == 0 && j == arr[i].length - 1) {
				newArr.push(arr[i]);
    }
  }
}
return newArr;
// Only change code above this line
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
  **Your browser information:**

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

Challenge: Iterate Through All an Array’s Items Using For Loops

Link to the challenge:

Hey there :wave:

From my experience, I would say not to stress yourself about it. It’s all part of the learning process and your ability to solve problems efficiently will continue to grow as you learn.

For my own part, nearly every time I look at my old code I see a way I could do it better. We’re constantly learning new techniques, new theories, and new concepts, so it only make sense that we will constantly be learning better ways to solve a problem. Some times I’ll have problem that I can figure out a super efficient way to solve, and other times I’ll be glad just to solve it at all, and come back to it later to improve it.

In short, don’t stress yourself out about. Know that solving problems efficiently comes with experience, and experience takes time. As long as you are learning, you’ll get there one step at a time.

4 Likes

That’s very common in the beginning. Heck sometimes even experienced coders do this. But the more you code, the more experience you get solving algorithms, the easier it will get and the more you’ll develop a “spidey sense” for the easiest solution. I used to teach guitar and to people wanting to learn to play solos I would always say, “In order to learn to play good solos, first you’ve gotta play a lot of bad solos.” It’s just part of the process. There is no magic pill.

Go easy on yourself. This is hard stuff.

4 Likes

This is very common, at every level. 40 years in development, one form or another, and I still find myself having to adjust my thinking. Often I’ll way over-engineer a solution, and applying some “soft skills” can show me where I wandered off course.

First, don’t think about this as a filtered array thing, create a story of sorts, a word problem rather than a programming one.

For example, in this problem think about a common list you might have in your day-to-day. A shopping list? A Christmas list? A list of homework assignments? Pick one, it’s a real world array.

Say we do the shopping list. But my cat just texted me, I don’t need to pick up cat treats or canary food - she solved both problems for me, it seems.

Now, in real life, list on paper and text from cat … How precisely do we remove items from that shopping list, or even determine if they’re on it? We do it all the time, but what are the exact steps we take?

Some folks have one way to do this, some have a different way, and both are valid. But this process of parsing how we think, even before we code that, is an essential skill.

How do we calculate three to the seventh power (3^7)? We think recursively, without really being aware we are.

In each function we are analyzing and organizing how we think, so we can teach a silicon block to take the same steps and reach the same result we would, *or we are teaching it to take our thought process and extend it somehow.

But it starts with consciously, formally thinking about how we think.

4 Likes

Hi,
First of all don´t worry and don´t be too hard to your self. I think the thing you have explained is simply caused by a lack of experience.
I´m sure that with the time you will grow and step by step you will notice that you will work more and more effective.

best regards,
Kai
Regards,
Kai

Getting to a “better” solution is also part of code refactoring. You might not get it “first try”.

Think about the problem, and get a working solution. Now, look at the code you wrote and think about the problem again.

Start with something simple like the variable names. Sometimes just coming up with new variable names can make you think more about what it is the code is doing. Maybe you change the count variable to seen and change the count == 0 to a boolean check !seen. Maybe that would remind you of some method you had worked with before, like includes (or indexOf).

In this case, it likely isn’t a coincidence that the challenge Check For The Presence of an Element With indexOf() was placed right before this challenge.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.