I am doing a challenge in codewars that deals with the following:
ATMs allow 4 or 6 digit PIN codes and PIN codes cannot contain more than exactly 4 digits or exactly 6 digits. If a valid PIN string is passed to the function, it returns true, otherwise it returns false.
Basically it has to be a 4 or 6 digit PIN, that is positive, that is not with a decimal but that it has to be an integer and that it only be numbers. My problem is that it returns me verdadero
in the decimal numbers when it would have to give falso
. Please if someone helps me and explains my mistake I would appreciate it
function validatePIN (pin) {
let num,result, long = pin.length, expresion = /.-+[a-z]/
long === 4 || long === 6 && !expresion.test(pin) ? num = parseInt(pin)
: result = false;
Number.isSafeInteger(num) && num >= 0 ? result = true
: result = false;
console.log(num)
return result;
}
Make sure what arrives is string:
Here is a simple test app that passes all the tests on their codewars page. It has three steps:
The code:
What it would actually do is explicitly convert the digits to a string and count the number of characters. If it's 4 or 6, check if the pin is an integer or a float.