Post Reply 
checkdigit calculation for HP-17b
07-29-2015, 08:30 AM
Post: #24
RE: checkdigit calculation for HP-17b
(07-29-2015 01:52 AM)Thomas Klemm Wrote:  This looks like duplicated code that could be extracted to a separate function.

Maybe something like this?
Code:
function checkCPF(cpf) {
    if (cpf == "00000000000") {
        return false;
    }
    return verify(cpf) && verify(cpf.substring(1));
}

function verify(value) {
    var CHECK = 9;
    var digit = value.split("");
    var sum = 0;
    for (var i = 0; i < CHECK; i++) {
        sum += digit[i] * (i + 1);
    }
    return (sum % 11) % 10 == digit[CHECK];
}

At least that makes it more apparent that you run the same verification twice.
The 2nd time just shifted by one digit.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: checkdigit calculation for HP-17b - Thomas Klemm - 07-29-2015 08:30 AM



User(s) browsing this thread: 1 Guest(s)