What do you think about my code which solved the challenge? And why do Freecodecamp solutions use methods that have not been taught so far in the curriculum?

Finally I completed this challenge after about 2 hours of trying different methods that had been taught before like arrays, loops, regex etc:
What do you think of it?
After I passed the challenge, I looked at the solutions and all four of them mentioned methods that had not been taught before. I wonder why is that so:
For example Three solutions out of four used the word split which has not been taught so far.

 let words = str.split(' ');  //split??

And one code used the map keyword which has also not been taught so far in the curriculum.

return Math.max(...str.split(" ").map(word => word.length)); //map?? Math.max??

I wonder why code that has alredy been taught so far isn’t being used.

You know the coursework is already a lot, the challenges aren’t easy either, I can’t go around learning every single of these things that aren’t in the curriculum for example a different type of recursion in the previous challenge called Tail Recursion and things like reduce, fill etc.

I hope it’s okay to not go ahead and start learning about all of these things that I find in the solutions/skip these new terms that have not been taught yet, and just stick to the curriculum. It will take forever, and I think I’ll go crazy if I go ahead and attempt to learn about all these things that are not in the curriculum when the curriculum itself is a lot to cover and solve.

  **My solution Code**

function findLongestWordLength(str) {
let reg = /(\w+)/g;
let a=str.match(reg);
let ctr=0;
for(let i=0;i<a.length;i++)
{
  
  if(a[i].length>ctr){
    ctr=a[i].length;

  }
}
return ctr;
}

console.log(findLongestWordLength("The quick brown fox jumped over the lazy dog"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 OPR/76.0.4017.154

Challenge: Find the Longest Word in a String

Link to the challenge:

@twotani What are your views on this?

Honestly, I’d make your solution without looking at the user provided solutions in the guide post. It can be helpful to then go back and look at guide post solutions to learn different ways to approach the problem.

People come to freeCodeCamp with different levels of knowledge, and the guide post solutions reflect this. They are not “official” solutions in any sense or “expected” solutions for all users. They often contain solutions with more advanced syntax for users who are using the introductory challenges as a way to practice advanced syntax.

It is expected that your code will probably be different than the guide post solutions.


As far as your solution goes, I would try to use more descriptive variable names and standard formatting to help make your code more readable and maintainable.

This is your code passed through a basic formatting program:

function findLongestWordLength(str) {
  let reg = /(\w+)/g;
  let a = str.match(reg); // What does a stand for?
  let ctr = 0; // What dose ctr stand for?
  for (let i = 0; i < a.length; i++) {
    if (a[i].length > ctr) {
      ctr = a[i].length;
    }
  }
  return ctr;
}
1 Like

Yes absolutely. It is because of your previous comments on someone’s post where you were strongly against looking at solutions after 15 minutes of trying that I resisted the urge to look even working on it got a while. So satisfied at the end with hard work and all.

I look at the solutions after I solved it but was frustrated seeing new terms and all but I’ll just skip them got now and stick to the curriculum otherwise it will be too overwhelming to learn so many new things when what’s on the plate is already a lot.

Thanks for the formatting suggestions. I didn’t bother as getting the logic correct was my main focus and this didn’t pay much attention. It’s not easy not too hard either but very exciting so far and enjoyable.

Yeah, I can see how it would be frustrating to see a bunch of solutions with syntax you don’t know yet. The next two chunks of course materiel, Object Oriented Programming and Functional Programming, will teach you a bunch of this new syntax, and then you’ll be able to practice it in Intermediate Algorithm Scripting or go back and practice reworking the Basic Algorithm Scripting challenges with the new syntax.

1 Like

I can’t wait for it to get over so that I can move on to the next certification lol so don’t think will be going back to the previous sections anytime soon lol

Your solution is on the right track but still over-complicated. I personally think the solution using .map() and .split() is the best and is how I would write it, but the challenge is doable in one pass without any functions (or ES6). Before you view my implementation, try to think of how you would keep track of current length and how you would know you’ve reached the end of a word.

function findLongestWordLength(str) {
  let longestLength = 0;
  let currentLength = 0;

  for (let i = 0; i < str.length; i++) {
    if (str[i] === " ") {
      if (currentLength > longestLength) {
        longestLength = currentLength;
      }
      currentLength = 0;
    } else {
      currentLength += 1
    }
  }

  // Check final word
  if (currentLength > longestLength) {
    longestLength = currentLength;
  }

  return longestLength;
}
1 Like

From a pedagogical standpoint, I agree that we never should expect learners to use something that is not taught. We don’t expect to take a test in a classroom setup that requires a solution with techniques not taught in the lectures. Here in the Wild West of Internet, however, you need to be a lot more flexible. I always suggest everyone not to rely on a single source for learning. You want two or three different sources to learn the subject matter. Different sites teach the subject matter in varying styles and focus. Pick the sites that are compatible with your learning style. They are pretty good ones out there.

Per your specific issue, I believe FCC curriculum is developed by multiple contributors. As such, there are often differences in the quality of explanation and the level of difficulty. As already mentioned, don’t worry about the posted solutions. The key is whether you can solve the problem. View FCC as a place to test your knowledge by solving challenge problems. The reason I suggest everyone to check multiple sites is that when you get stuck, consulting other sites may lead you to a solution. And be able to consult multiple places to find a working solution is a must-have capability in a real world.

And there’s always this Forum for you to ask questions. Although I’m fairly new to this Forum (maybe two months), I can tell this Forum is very active and posters get help and needed answer almost immediately. I was saved a few times by posting questions here when I couldn’t tell why my code is not passing some tests. And, as much as I can, I try to help others by answering questions.

2 Likes

I’m honestly surprised how late in the curriculum split is taught. I’m not sure, but I think we may have moved the Functional Programming part at one point, so it comes later in the course now. But I really don’t remember so I might be wrong.

In any case, I don’t see why we don’t teach split and join sooner. It’s just build-in methods and has very little to do with functional programming.

2 Likes

This was what I was trying initially that is using loops only but was not able to solve it. I was getting frustated so I moved on to my another thought: regexes.

Nice to see it solved using loops. Thank you for the answer.

Thank you. Will do that.

I completed my first certification last year and than began Javascript certification immediately but stopped when I reached the middle of ES6. After a whole year gap I am finally back. It feels good to be back, its exciting, fun etc Hopefully things won’t get too overwhelming and I can continue on to finish the certification and than the ones that come after it with the hope of making my own apps, good sites etc one day and landing a home based/remote developer job.

I too think there has been some changes to the curriculum as I came back after a one year gap like I think there was an advanced algorithm scripting last year. I may be wrong. But it’s good to have things updated and introduced slowly so that we won’t get overwhelmed.

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