Are any of the JS tutorials correct!?!?

Tell us what’s happening:
In almost every single lesson I find errors in the lessons. It can be the description is wrong or the task that needs to be performed is poorly written. Then when you click the “get hint” even the solutions are wrong most of the time. Take this one for example. My solution is below and passes the validation checks however I decided to see how it compared to the official solution. (Read the challenge tasks)

Your code so far


let sampleWord = "astronaut";
let pwRegex = /^[a-z](?=\w{5,})(?=\D*\d\d+)/i; // Change this line
let result = pwRegex.test(sampleWord);

Mine passes fine but below is the offical solution and why it is wrong.

Official Solution

^(?=\w{6})(?=\D+\d{2})

Let’s translate this. Right off the bat, the very first character is wrong, it says \w which matches letters and numbers yet the instructions clearly say it must start with letters only. So this should be [a-zA-Z]. And then it says it must be greater than 5 chars but the solution has {6} which matches EXACTLY 6 when it should be {6,} for 6 or greater…

I’m a noob at JS and Regex. Now if I can spot these mistakes straight away then how can I be confident in the skill level for who writes the solutions??

Challenge: Positive and Negative Lookahead

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/positive-and-negative-lookahead

You are not understanding what the lookaheads are doing in this solution. Both of the groups must match. The second lookahead says the first one or more characters is not a digit followed by 2 digits.

ok so can you please break each part down as I still don’t understand. I get the second group, but the first group says ^ as in start with then followed by \w which means digits too!??

The starting position of both of the positive lookups is the first character. The first one uses the \w{6} which means the first 6 characters can be a number or letter. However, since there is another positive lookup, it gets factored into the final match. Putting the two together is like stating:

Starting from the first character, only match if (the first 6 characters are letters or numbers) AND (the first one or more characters is not a number but is immediately followed by two numbers) with character case ignored.

I wrapped the relative groups with parentheses in my explanation.

ok I see what you are saying. When you explain it like that it makes sense. But the lesson doesn’t make that clear. From previous lessons it implies that the first bracket must match first (as in first 6 chars are alphanumeric) then AFTER those chars it evaluates the second bracket stating that 1 more or non digits followed by 2 digits (in other words it’s sequential). If this is not how the syntax works then it should be clearly explained like you just did.

So the solution is correct but as I said in my opening statement it is either the solution or descriptions or lessons that are wrong. IMO the lesson doesn’t explain this syntax enough; the lesson clearly explains look ahead (what it’s about) but not about that two sets of brackets means AND. Or am I understanding this wrong too?

A more practical use of lookaheads is to check two or more patterns in one string. Here is a (naively) simple password checker that looks for between 3 and 6 characters and at least one number:

The above is near the end of the challenge description section (above the last example). The word and implies that when using two patterns (as seen in the last example), both must match. If you have a suggestion for how to better word the challenge, please feel free to add your comments to this existing issues raised back in October 2019 for this challenge.

1 Like

There is no AND in that statement. I think because you understand how it should work, you read the word AND in there but it actually isn’t there lol, By stating “check two or more patterns” it still isn’t clear that both patterns must match the string from start to finish, it could be as I interpreted it (like previous lessons) where the pattern matching was sequential.

Regardless, you’ve cleared it up for me, I might add to the issue you linked to. thanks,