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.