Positive and Negative Lookahead Regex- JS

TLDR; The RegEx test() method and the freeCodeCamp tests (code case checker)
are two different things.

test() returns true if the Regex pattern matches a pattern in a given string and returns false if there isn’t a match.

The FCC code checker tests that the return value is what it is expected to be: true when it should be true and false when it should be false, which is how it determines if the code passes a particular set of cases or not.

Not sure what you mean by:

I tested your code…

let sampleWord = "12345";
let pwRegex =  /(?=\w{5,})(?=\D*\d{2})/;  // Your Code
let result = pwRegex.test(sampleWord);

The console output when clicking “Run The Tests” is:

// running tests
 Your regex should not match the string 12345
// tests completed

I DO NOT get the popup box below for your code…

pass

So actually, it doesn’t pass. If you are getting the popup window for the code you posted above, then I’m totally baffled and not exactly sure what you are doing.

I also went back and tested the “Official Solution” on the Hints page
and all of these input vs pattern cases pass the FCC code check for the Official FCC solution:

let result = pwRegex.test(1234567);  //returns true
let result = pwRegex.test(123456);   //returns true
let result = pwRegex.test(12345);    //returns false
let result = pwRegex.test(1234);     //returns false
let result = pwRegex.test(123);      //returns false
let result = pwRegex.test(12);       //returns false
let result = pwRegex.test(1);        //returns false

Why does FCC 's code check pass the ones that return false? …

The FCC code check determines whether or not the code returns a specific boolean value that it expects to be returned for several different cases.

The return value will only match what the code check expects if you have used a correct RegEx pattern.

Your code returns true for let sampleWord = "12345"; because case 12345 matches your pattern, but the code check expects the return value for case 12345 to be false.

Your pattern doesn’t satisfy the challenge requirement and so your code should not pass the code check for case 12345, and it doesn’t.

The FCC solution code returns false for let sampleWord = "12345"; because case 12345 doesn’t match the solution code’s pattern. The code check expects the return value for case 12345 to be false.

The solution pattern satisfies the challenge requirement and so it should pass the check for case 12345, and it does.

The return value (a boolean in this challenge) is letting us know whether the search pattern finds a match in the input string or not.

true means the pattern found a match, false means the pattern did not find a match.

Whether the return value is true or false doesn’t necessarily mean it should or shouldn’t pass the FCC code check. Rather, does it return true when it expects true and false when it expects false? If not, that means your RegEx pattern is incorrect.

So the FCC code check takes all of that into account.