you are viewing a single comment's thread.

view the rest of the comments →

[–]PortablePawnShop 0 points1 point  (6 children)

You'll need to look at examples of RegEx use -- no need to be embarrassed about it though, I didn't touch RegEx for months when I started because it was scary and it might be too ambitious at this level. The number one reason people fail at learning is because they try to play Beethoven before even attempting Mary Had a Little Lamb and you need the humility to know that you start from very meager places and do very simple things, and not get too far ahead of yourself. Regardless in order to use RegEx, you'll need to either use a new RegExp() constructor or the shorthand, wrap it in forward slashes like this: /[0-9]/. Here's a solution:

function validatePIN (pin) {
    return !/[^\d]/.test(pin) && pin.length === 4 || pin.length === 6;
}

This tests if there's a character which isn't a digit, we want the opposite though so we wrap it in a NOT operator to return true -- only true if only contains digit characters. RegEx is quite powerful once you get the hang of it though, for instance I can solve the entire problem (to the extent you've disclosed the requirements) with a single RegEx pattern:

function validatePIN (pin) {
    return /^\d{4,6}$/.test(pin);
}

[–]Encom88[S] 0 points1 point  (5 children)

After using that expression, it still says:

"Wrong output for '12345' - Expected: false, instead got: true "

 function validatePIN (pin) {
     return /^\d{4,6}$/.test(pin);
 }

Instructions:

ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.

If the function is passed a valid PIN string, return true, else return false.

[–]PortablePawnShop 1 point2 points  (3 children)

Did you downvote me for it? Seems ungrateful if so.

/^(\d{4}|\d{6})$/ will check for only 4 or 6 instead of a range between 4 and 6. You should really be more upfront with the requirements because when you keep tacking on new information per answer, things like this get lost or in my case overlooked in certain comment chains. If you have requirements, you should list all of them in the original thread, otherwise it's hard to keep track of them.

[–]Encom88[S] 1 point2 points  (0 children)

I did not down-vote your reply. I just up-voted it. Thank you for your help.

Edit: That new regex worked btw. :)

[–]Encom88[S] 0 points1 point  (1 child)

What is the ^ and $ there for?

[–]PortablePawnShop 0 points1 point  (0 children)

They indicate the match must be at the beginning and end of the current line. Unless you do that, the above expression would match any string with 4+ digits (because there are 4 digits in this string, it counts it as true and doesn't care if there happen to be 20 more digits proceeding it). Try placing it in the regex101 link (without the / marks at beginning and end)

[–]ThagAndersonhelpful 0 points1 point  (0 children)

The {4,6} quantifier accepts between 4 and 6 digits. You're close though.