Home > Blockchain >  Convert Modulus 11 Self-Check Digits PHP code to JavaScript
Convert Modulus 11 Self-Check Digits PHP code to JavaScript

Time:07-02

Using Modulus 11 Self-Check Digits PHP module.

http://www.eclectica.ca/howto/modulus-11-self-check.php

function GenMOD11( $base_val )
{
   $result = "";
   $weight = array( 2, 3, 4, 5, 6, 7,
                    2, 3, 4, 5, 6, 7,
                    2, 3, 4, 5, 6, 7,
                    2, 3, 4, 5, 6, 7 );

   $reversed_base_val = strrev( $base_val );
   for ( $i = 0, $sum = 0; $i < strlen( $reversed_base_val ); $i   )
   {
      $sum  = substr( $reversed_base_val, $i, 1 ) * $weight[ $i ];
   }

   $remainder = $sum % 11;
   switch ( $remainder )
   {
   case 0:
      $result = $base_val . 0;
      break;
   case 1:
      $result = "n/a";
      break;
   default:
      $check_digit = 11 - $remainder;
      $result = $base_val . $check_digit;
      break;
   }

   return $result;
}

Tying convert php code to JavaScript. This is my code , but it not give correct result:

<script type='text/javascript'>
jQuery(document).ready(function($) {


 function GenMOD11F( base_val )
    {
        
    var result = "";
    var weight = [ 2, 3, 4, 5, 6, 7,
                        2, 3, 4, 5, 6, 7,
                        2, 3, 4, 5, 6, 7,
                        2, 3, 4, 5, 6, 7 ];
    var reversed_base_val =  parseInt(base_val.toString().split('').reverse().join(''));
 
    var i, sum;
   for ( i = 0, sum = 0; i < reversed_base_val.length; i   )
   {        
      sum  = reversed_base_val.substring(i, 1)  * weight[i];
   }
   
   var remainder = sum % 11;
   switch ( remainder)
   {
   case 0:
      result = base_val 0;
      break;
   case 1:
       result = "n/a";
      break;
   default:
      var check_digit = 11 -  remainder;
      result = base_val.check_digit;
      break;
   }

   return  result;
    }
});
  </script>

For ex. When using php function

GenMOD11(123240657) it gives 1232406573

Using JavaScript GenMOD11(123240657) it gives 123240657

CodePudding user response:

This code should give you the required result.

Some notes:

  • We convert the input to string immediately to ensure the rest of the code works correctly.
  • Rather than reverse the base_val variable string, we convert to an array and leave it as such, this simplifies the rest of the code.

function GenMOD11(base_val)
{
   base_val = String(base_val);
   const weight = [ 2, 3, 4, 5, 6, 7,
                    2, 3, 4, 5, 6, 7,
                    2, 3, 4, 5, 6, 7,
                    2, 3, 4, 5, 6, 7 ];

   const reversed_base_val = [...base_val].reverse();
   let sum = 0;
   for(let i = 0; i < reversed_base_val.length; i  ) { 
       sum  = reversed_base_val[i] * weight[i];
   }
   const remainder = sum % 11;
   switch ( remainder )
   {
       case 0:
          return base_val   0;
       case 1:
          return "n/a";
       default:
          check_digit = 11 - remainder;
          return base_val   check_digit;
   }
}

const testInputs = [123240657, 31, 445, 5800];
for(let input of testInputs) { 
    console.log(`GenMOD11(${input}):`, GenMOD11(input))
}
.as-console-wrapper { max-height: 100% !important; }

  • Related