Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. const m1 = Array.from({ length: 11 }, (k, v) => v)
  2. .reverse()
  3. .slice(0, 9)
  4. const m2 = Array.from({ length: 12 }, (k, v) => v)
  5. .reverse()
  6. .slice(0, 10)
  7.  
  8. let tempCpf;
  9. let digito;
  10. let soma;
  11. let resto;
  12.  
  13. cpf = cpf.trim();
  14. cpf = cpf.replace(".", "").replace("-", "");
  15.  
  16. if (cpf.length != 11 || /([0]{11})|([1]{11})|([2]{11})|([3]{11})|([4]{11})|([5]{11})|([6]{11})|([7]{11})|([8]{11})|([9]{11})/.test(cpf))
  17. console.log({ 1: false });
  18.  
  19. tempCpf = cpf.substring(0, 9);
  20. soma = 0;
  21.  
  22. for(let i=0; i<9; i++)
  23. soma += parseInt(tempCpf[i].toString()) * m1[i];
  24.  
  25. resto = soma % 11
  26. resto = resto >= 2
  27. ? 11 - resto
  28. : 0
  29.  
  30. digito = resto.toString();
  31. tempCpf = tempCpf + digito;
  32. soma = 0;
  33.  
  34. for(let i=0; i<10; i++)
  35. soma += parseInt(tempCpf[i].toString()) * m2[i];
  36.  
  37. resto = soma % 11
  38. resto = resto >= 2
  39. ? 11 - resto
  40. : 0
  41.  
  42. digito = digito + resto.toString()
  43.  
  44. console.log({ 2: cpf.endsWith(digito) })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement