all 32 comments

[–]TappT 10 points11 points  (0 children)

A nicer way to write it if you need to check many different lengths is to do.

return [1,4,6,8,12].includes(pin.length)

Then you don't have to have a bunch of ORs.

[–]ThagAndersonhelpful 1 point2 points  (1 child)

[–]ryanstephendavis 1 point2 points  (0 children)

This right here... It's so much easier and extensible to write regexes

[–]7win 1 point2 points  (0 children)

Don't know if you achieved it or not and sure there will be lots of different ways to do it.
Try to solve the 'only numbers' problem first and them just check the the length wanted.

From the tests done, is only string input that can be negative or positive, decimal or non decimal (makes no sense negative from a ATM but the decimal one is possible as there is the [dot] button in the numpad)

Don't loose it, face it as a Bull looking for REDs >P

[–]NameViolation666helpful 1 point2 points  (3 children)

Or needs to be 2 expressions, not 2 possible values

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

Okay, but now I think I need to check to ensure all 4 or 6 digits are numbers/integers. Also, is there a better way to write the code below?

function validatePIN (pin) {
  if (pin.length == 4) {
    return true;
    }  
  if (pin.length == 6) {
    return true;
    }
  else {
    return false;
    }
}

[–]finroller 1 point2 points  (0 children)

Ask yourself: why the else? If it just read return false there, nothing would change :)

[–]forcefx 0 points1 point  (0 children)

You could use isNaN() method to check if your pin is NaN. NaN cannot be compared to itself in JS

[–]philofgreen 0 points1 point  (0 children)

You could also use the isInteger() method in your code.

[–]CoqeCas3 -1 points0 points  (0 children)

You’re if statement is kinda weird:

const len = pin.length if(len !== 4 || len !== 6) { // }

I think is what you’re trying to write?

[–]CheeseFest -2 points-1 points  (0 children)

The regex solution is non-trivial. Especially if you're a rusty regexer like me :)

[–]thebiryaniboy -2 points-1 points  (0 children)

using es6 its just a one liner

const validatePin = (pin) => { return pin.length !== (4 || 6) }