Task: 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.
My solution:
function validatePIN (pin) {
//return true or false
if (!isNaN(pin) && Number.isInteger(pin) && pin.toString().length == 4 || pin.toString().length == 6) {
return true
} else {
return false
}
}
The only bug I get is when I pass 4 digits as a string ("1234"
) - it equals false
.
function validatePIN (pin) { return typeof pin === 'string' && // verify that the pin is a string Number.isInteger (+pin) && // make sure that the string is an integer when converted into a number [4, 6].includes (pin.length) // only accepts 4 and 6 character pins } Show activity on this post.
Create a regular expression to validate pin code of india as mentioned below: regex = "^[1-9]{1}[0-9]{2}\\s{0, 1}[0-9]{3}$"; Where: ^ represents the starting of the number. [1-9]{1} represents the starting digit in the pin code ranging from 1 to 9. [0-9]{2} represents the next two digits in the pin code ranging from 0 to 9.
First digit of the pin code must be from 1 to 9. Next five digits of the pin code may range from 0 to 9. It should allow only one white space, but after three digits, although this is optional. The given number satisfies all the above mentioned conditions. The given number satisfies all the above mentioned conditions.
Task: 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.
function validatePIN (pin) {
return typeof pin === 'string' && // verify that the pin is a string
Number.isInteger(+pin) && // make sure that the string is an integer when converted into a number
[4, 6].includes(pin.length) // only accepts 4 and 6 character pins
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With