I'm working on a codewars kata that uses the Luhn Algorithm to validate credit card numbers. Already have my answer, which uses string conversion to split the digits and then reconvert to an integer and then use the rest of the algorithm. All well and good until I test it on an octal number.
The code starts as:
def validate(n)
n.to_s.split("") #n = 7867 gives me ["7","8","6","7"], which is fine
n.to_s.split("") #n = 0776 gives me ["5","1","0], when I need ["0","7","7","6"]
n.to_s.split("") #n = 0100 gives me ["6", "4"] when I need ["0","1","0","0"]
#other code here
end
where the method should be called on whatever n is plugged in.
How do I prevent an octal, binary, or hexidecimal, etc number from converting like that? Is there a way to keep the digits as is so I can use them?
nfrom?nis just whatever number gets plugged into the method.def validate(n)0until I plugged in0905and the error readinvalid octal numberand I learned octals were even a thing.