I've mentally crashed at JavaScript Practice: Arrays, Loops, Objects, Iterators

This page consists of 3 exercises that don’t have a hint button, only “view solution” after you’ve submitted your answer a few times. My issue isn’t specifically concerning the questions I can’t answer, but I need some direction based on how lost I feel right now. I’ve gone through this whole section on JavaScript and have felt OK about it, I struggle here and there but between Googling, AI help, MDN, etc, I find a way to come up with an answer or at least understand the solution so I can take another run at it on my own and succeed.

That said, I’ve really hit a wall on these exercises and they make me feel like I’m not at all where I’m supposed to be. Here’s an example -

Write a function subLength() that takes 2 parameters, a string and a single character. The function should search the string for the two occurrences of the character and return the length between them including the 2 characters. If there are less than 2 or more than 2 occurrences of the character the function should return 0.

Examples:

subLength('Saturday', 'a'); // returns 6
subLength('summer', 'm'); // returns 2
subLength('digitize', 'i'); // returns 0
subLength('cheesecake', 'k'); // returns 0

Here’s the given solution -

// Write function below
const subLength = (str, char) => {
let charCount = 0;
let len = -1;

for (let i=0; i<str.length; i++) {
if (str[i] == char) {
charCount++;
if (charCount > 2) {
return 0;
}
if (len == -1) {
len = i;
} else {
len = i - len + 1
}
}
}
if (charCount < 2) {
return 0;
}

return len;
};


I never would have come up with creating 2 variables (charCount and len) on my own to begin the process. I feel like this should be intuition but I’m just not there. I felt OK about everything so far, and now that the course is bringing together all the different subjects we tackled, I’m completely dumbfounded. I feel like I’ve taken guitar lessons and learned 3 chords, and now I’m being asked to play a solo. I don’t want to blame the course, I’d prefer to take responsibility on my own, but where do I start to improve? Should I just go back and start this JavaScript section all over again and hope it sinks in deeper this time? Should I have AI break down the solution for me, plug it in as if i completed it and move on, hoping that more will come together later? I’m just really lost, but I’m willing to put in the work, and I want to succeed. I just have no one to turn to in the real world and could use some advice. I’m ready to apply more effort, and I wont give up, but I think I just need some direction. Can I get some advice?

Thanks everyone.

2 Likes

Especially this exercise is a nice one, because it is not complex. Three short sentences are enough to explain exactly what you are supposed to do. Yet it allows a great range of different approaches.

So you could go though the sample approach line by line with the help of AI in order to fully understand what the code does. And then the exercise still isn’t spoilt. Once you understand the sample code, you can still come up with a completely different approach.

One possible way would be trying to get away from thinking in code syntax for the idea process. Instead, try to think of a way how you would solve that with pen and paper:

Which informations do you have:

  • 2 parameters – a string (word) and a character

What are you supposed to do:

  • Count the characters between to occurrances of the given characters

How could you do that on a piece of paper:

  1. You could write down the word.
  2. Starting from the left, you could then cross out each letter that is not the given character.
  3. When you get to the first occurrence of the given character, you could draw a line on a tally sheet with the label ‘Occurrences of given character’
  4. Then you could make a line for each letter coming after that first occurrence that is not the given character on a tally list that you label as ‘Amount of Letters between’
  5. At the end you count the amount of lines on the occurrences list. If it is not exactly 2, you write ‘0’ on the last line. If it is 2, you count the amount of lines in your second list and write down that number.

With this approach, you would have 2 tally lists, that you would save in 2 variables:

let occurrencesOfGivenCharacter = 0;
let amountOfLettersBetween = 0;

Then you would start to go through your word character by character. You can search the MDN docs for methods, go back to the iterators lesson in CC or question AI. A for loop would work as well as the forEach() method or a while loop. Inside the loop or iterator, you could increase the variables accordingly. Or you could count the occurrences of the characters in the given word with the reduce() method. There are plenty of possible ways to do that. You could start as well from the left and from the right at the same time with crossing out letters. Think of analogue ways first and then try to transcript them to code.

As a summary, I think there are many ways to Rome and they are all pretty. Take whatever suits you first. And then take another. In the end, you need to understand different approaches and be able to come up with your own ones. But how you get there and how long it takes is up to you. Nothing is better or worse.

4 Likes

Hey, I really appreciate the reply, and I wouldn’t have picked up a pen and paper but your answer seems very logical and would definitely help me to “dumb it down” for myself. When I think syntactically, I definitely become overwhelmed easily and I think this method would reduce some intimidation. More important to me than the solution itself, or even finding the solution, is the mentality to maintain during all steps of the problem solving process.

Thanks so much for your help!

2 Likes

Just wanted to say thank you for posting this. I’ve hit a similar mental block at these three challenges as well as the 14 challenges before them and have been feeling super discouraged. Most everything leading up to this point had felt very comfortable but now it’s like I can’t bend my brain the right way to solve these problems. I check the solutions after failing to solve and find that I’ve inevitably over-complicated my code :face_with_spiral_eyes: Were you able to push through and come out the other side with everything making more sense? Does this get easier?

1 Like

Yes, and no. The basic stuff becomes second nature, but the problems might get harder. First we learn how to use the basic tools, which is taxing enough to begin with. Finding it does get easier to use those tools makes tackling what comes next a little easier—the problems yet to present themselves.

Learning a language is one thing; all we really do is memorize or at least be aware of the promises made by the language. Pure algebra at that point. Ideation and implementation of code models is where the difficulty arises. Please do not be straddled by problems with the language, at this point. It is so critical that your mind is on the problem, not issues with syntax/usage.

We can only deduce that problem solving becomes easier with experience, albeit reduced to how we approach it. Some problems may never be adequately solved, or not solved to our satisfaction. Getting to accept that it never gets easier does get easier, with time and practice.

Put another way, the only thing that gets easier is accepting that it never gets easier to solve problems. They will always present their own brand of difficult challenges. At this point we begin to accept our own place in the big picture. Do we solve problems; or, do we just talk about them?

2 Likes

I have a friend who started this same career path, so i went back and did a bunch of CSS that led up to this point, and I decided to do JS syntax I and II all over again. I’ve just begun JS syntax part II, and its just beginning to ramp up once again. I can say, I forgot pretty much all syntax but as soon as I see an example it comes back quickly, and I’m also remembering other methods I can use that are helping me out. The first time around was very overwhelming; this time is probably going to be pretty similar, BUT, I’m remembering tools that I picked up from the last run and I’m not as intimidated. I did a project yesterday with nested for loops and it actually made sense this time rather than kind of looking at it and thinking ‘ok…’.

So unfortunately I can’t really tell you if it ‘got better’, because I took a huge step backward and kept working to where I crashed, and I’m going to be back there soon. Feel free to keep me posted on your own progress, where you are, what projects you’re on, and I can do the same.

I’ll tell you this - I’ve been a guitar player since I was 13 years old (I’m 37 now), and I’ve conquered some challenging pieces of music that I NEVER thought I could pull off, and when I talk about theory around my music buddies they look at me as if I’m from another planet, as if I spend my off-time in a lab to pump my brain up. It’s really not so bad, its repetitions, focus, hard work, and not giving up. I’m not the best guitar player, or the smartest, nothing feels like it comes natural. But I can work like an absolute dog and take a mental beating like nobodies business. I have convinced myself that code is no different. I’m genuinely interested in it, I think its fascinating and cool. I love how creative you can be, and how logical you can be. The desire is there. The work ethic is there. The interest is there. So there’s really nothing that can stop us, other than ourselves. It won’t come easy, but I truly believe that if your desire is pure and you don’t give up, you will absolutely succeed. Keep up the hard work!!

2 Likes

Thank you, mtf, for your kind response. I’m now making my first pass at the Credit Card Checker and it has been a comfort to think about difficulty or frustration as a necessary part of the learning process. I will have to come to terms with discomfort :slightly_smiling_face: I feel less discouraged now by my inability to do what I’d actually like to with JavaScript since I can at least understand the problem in front of me and formulate my response in plain English.

2 Likes

What a wonderful way you have of relating this struggle to your experience as a musician. I so appreciated your 3 cords and a solo analogy that I was able to explain how I felt about these very challenges to my husband in terms he could better understand :laughing: I’m of a similar age and non-tech background and would love to be coding buddies. It’s nice to know there’s someone else out there having the same doubts/difficulty but as you said (and I fully agree), there’s nothing that can stop us but ourselves. I hope JS syntax part II is treating you well. Persistence surely must be key!

1 Like

Ok, I’m back to the dreaded challenges, haha. I just want to say that this is incredibly challenging all over again even after repeating all of JS II. However, I came in with a bit more confidence, and definitely more competence this time. Although I’m proving myself to be highly ineffective so far (I’m really bombing these challenges - I get completely lost, build long, inefficient, ineffective blocks of code that don’t work… I google, I use AI to break it down, I “get it”, then I go try and write it myself and I’ll get it after a few syntax errors. But I don’t really get it. I’m not good enough to piece together the parts I need to solve the problem. It’s not a matter of not knowing how to create a function, or increment, or loop, etc… it’s having intuition and coming up with ideas to solve the problems you’re given, even if they seem a little silly or weird.

With that said, I feel a bit less bad about being a dumpster fire of a student, because I think this lesson is not mainly focused on implementing code, but it’s really letting go of your hand, and pushing you away to let you figure it out on your own.

At this point, I’m just going to keep trying, and maybe repeating a few times. I’m not sure if this is something I should move on from after a few reps? It’s good to practice, but if I practice the same handful of problems, I’m going to fall victim to memorization and use muscle memory rather than critical thinking. Sorry if I sound like a downer! I’m feeling the sorrow right now but I’m about to dust off and get back to it. Good luck!

1 Like

I’ve had the same experience. I did my second pass of JS II and made it to the other side of a few of the challenge problems, but some of them were still beyond my thought process. I’d have that “tip of the tongue” almost there sensation and then continue to labor without solving or solving only after AI assistance. I guess this is the struggle? Training oneself to think logically and algorithmically without maybe having that tendency… it’s challenging for sure. I have to feel like it really is all about exposure and practice. I had a similar thought about not wanting to “cheat” through the challenges by repeating them too many times, so I’ve shelved them and found a few free resources to try to keep myself immersed in JS. FreeCodeCamp has been useful to keep me chugging along with basic JS and there’s an online site, JavaScript.info, that’s basically an interactive ebook starting from basic JS and going through advanced. I felt discouraged to the point of stopping when I quagmired at the credit card checker, so these have at least helped me maintain my commitment.

I’ve just finished the git section on this site (which was delightful after feeling hung up in the challenges) and now it’s time for the first portfolio project. I suppose this is where the rubber meets the road etc etc. I’m just trying to move forward with faith my brain will adapt as we go. Keep going, Steve! :fist_right:

1 Like