Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. function TCKN(number) {
  2. return {
  3. toString: function() {
  4. if(!this.isValid()) throw new Error('TCKN hatalı!');
  5. return this.no();
  6. },
  7.  
  8. isValid: function() {
  9. var first9 = this.first9();
  10. var tenth = this.tenth(first9);
  11. var eleventh = this.eleventh(first9 + tenth);
  12. return this.no() == first9 + tenth + eleventh;
  13. },
  14.  
  15. no: function() {
  16. if(number.toString().split('').length < 9) throw new Error('TCKN hatalı!');
  17. return number.toString();
  18. },
  19.  
  20. first9: function() {
  21. return this.no().substring(0, 9);
  22. },
  23.  
  24. tenth: function(first9) {
  25. var oddSum = parseInt(first9.charAt(0)) + parseInt(first9.charAt(2)) +
  26. parseInt(first9.charAt(4)) + parseInt(first9.charAt(6)) + parseInt(first9.charAt(8));
  27. var evenSum = parseInt(first9.charAt(1)) + parseInt(first9.charAt(3)) +
  28. parseInt(first9.charAt(5)) + parseInt(first9.charAt(7));
  29. return ((oddSum * 7 - evenSum) % 10).toString();
  30. },
  31.  
  32. eleventh: function(first10) {
  33. var total = 0;
  34. for(var i = 0; i < first10.split('').length; i++)
  35. total += parseInt(first10.charAt(i));
  36. return (total % 10).toString();
  37. },
  38.  
  39. first5: function() {
  40. return this.no().substring(0, 5);
  41. },
  42.  
  43. last4: function() {
  44. return this.no().substring(5, 9);
  45. },
  46.  
  47. next: function() {
  48. var first5 = (parseInt(this.first5()) - 6).toString();
  49. var last4 = (parseInt(this.last4()) + 2).toString();
  50. while(true) {
  51. if(last4.split('').length == 4) break;
  52. last4 = '0' + last4;
  53. }
  54.  
  55. var first9 = first5 + last4;
  56. var tenth = this.tenth(first9);
  57. var eleventh = this.eleventh(first9 + tenth);
  58. return new TCKN(first9 + tenth + eleventh);
  59. },
  60.  
  61. prev: function() {
  62. var first5 = (parseInt(this.first5()) + 6).toString();
  63. var last4 = (parseInt(this.last4()) - 2).toString();
  64. while(true) {
  65. if(last4.split('').length == 4) break;
  66. last4 = '0' + last4;
  67. }
  68.  
  69. var first9 = first5 + last4;
  70. var tenth = this.tenth(first9);
  71. var eleventh = this.eleventh(first9 + tenth);
  72. return new TCKN(first9 + tenth + eleventh);
  73. }
  74. };
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement