V
V
Vitaly2017-10-23 15:38:27
JavaScript
Vitaly, 2017-10-23 15:38:27

How to rewrite this code from Delphi to JS?

All the best! Please help translate this code from Delphi to javascript:

unit ipscrpt;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Dialogs, StdCtrls;

  function IpsEncodeString( source: string ): string;
  function IpsDecodeString( source: string ): string;

type
arr = array[0..9, 0..9] of integer;

const mas1: arr = (
      (1, 2, 3, 6, 0, 8, 5, 4, 7, 9),
      (2, 1, 8, 6, 5, 9, 7, 0, 4, 3),
      (7, 6, 1, 5, 8, 4, 2, 9, 3, 0),
      (2, 6, 5, 7, 4, 0, 8, 1, 3, 9),
      (0, 8, 6, 2, 7, 9, 4, 5, 3, 1),
      (5, 7, 8, 0, 6, 9, 2, 4, 3, 1),
      (0, 6, 5, 3, 8, 7, 1, 2, 9, 4),
      (9, 7, 8, 6, 3, 0, 1, 5, 2, 4),
      (1, 0, 5, 9, 2, 7, 3, 6, 4, 8),
      (6, 2, 9, 4, 1, 0, 8, 5, 3, 7)
);

const mas2: arr = (
      (4, 0, 1, 2, 7, 6, 3, 8, 5, 9),
      (7, 1, 0, 9, 8, 4, 3, 6, 2, 5),
      (9, 2, 6, 8, 5, 3, 1, 0, 4, 7),
      (5, 7, 0, 8, 4, 2, 1, 3, 6, 9),
      (0, 9, 3, 8, 6, 7, 2, 4, 1, 5),
      (3, 9, 6, 8, 7, 0, 4, 1, 2, 5),
      (0, 6, 7, 3, 9, 2, 1, 5, 4, 8),
      (5, 6, 8, 4, 9, 7, 3, 1, 2, 0),
      (1, 0, 4, 6, 8, 2, 7, 5, 9, 3),
      (5, 4, 1, 8, 3, 7, 0, 9, 6, 2)
);

implementation

function IpsEncodeString( source: string ): string;
var
  i,j,k: integer;

begin

  for i:=length(source) downto 1 do
  if not(source[i] in ['0'..'9']) then delete(source,i,1);

  result := '';

  i := 0;
  j := 0;

  while (i <= Length(source) - 1) do begin

    k := mas1[j, StrToInt(source[i+1])];
    result := result + IntToStr(k);

    inc(i);
    inc(j);
    if (j>=10) then j := 0;
  end;
end;


end;

end.

I need exactly IpsEncodeString . function.
I see Delphi for the first time, at first glance it's nothing complicated, but something doesn't work for me....

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bqio, 2017-10-23
@bqio

Try: https://jsfiddle.net/onxeb2st/2/

A
Abcdefgk, 2017-10-23
@Abcdefgk

"Comparchuchos!" Vulgarity.
- What an interesting language-y-yk!
So the gallery?

var array = [
  [1, 2, 3, 6, 0, 8, 5, 4, 7, 9],
  [2, 1, 8, 6, 5, 9, 7, 0, 4, 3],
  [7, 6, 1, 5, 8, 4, 2, 9, 3, 0],
  [2, 6, 5, 7, 4, 0, 8, 1, 3, 9],
  [0, 8, 6, 2, 7, 9, 4, 5, 3, 1],
  [5, 7, 8, 0, 6, 9, 2, 4, 3, 1],
  [0, 6, 5, 3, 8, 7, 1, 2, 9, 4],
  [9, 7, 8, 6, 3, 0, 1, 5, 2, 4],
  [1, 0, 5, 9, 2, 7, 3, 6, 4, 8],
  [6, 2, 9, 4, 1, 0, 8, 5, 3, 7]
];

ipsEncodeString(array);

function ipsEncodeString(array) {
  array.forEach(function(arr) {
    var res = '';
    for(var i=0; i<arr.length; i++) {
      if(arr[i] < 0 || arr[i] > 9) {
        arr.splice(arr[i], 1);
      } else {
        res += arr[i];
      }
    }
    console.log(res);
  });
}

V
Vitaliy, 2017-10-24
@Scorpiored88

At the moment, the most suitable option that came to mind:

var mas1  = [
    [1, 2, 3, 6, 0, 8, 5, 4, 7, 9],
    [2, 1, 8, 6, 5, 9, 7, 0, 4, 3],
    [7, 6, 1, 5, 8, 4, 2, 9, 3, 0],
    [2, 6, 5, 7, 4, 0, 8, 1, 3, 9],
    [0, 8, 6, 2, 7, 9, 4, 5, 3, 1],
    [5, 7, 8, 0, 6, 9, 2, 4, 3, 1],
    [0, 6, 5, 3, 8, 7, 1, 2, 9, 4],
    [9, 7, 8, 6, 3, 0, 1, 5, 2, 4],
    [1, 0, 5, 9, 2, 7, 3, 6, 4, 8],
    [6, 2, 9, 4, 1, 0, 8, 5, 3, 7]
]

function IpsEncodeString(source) {
    var i, j, k;

    source = source.replace(/\D/g,"");

    result = '';
    i, j = 0;

    for (i = 1; i <= source.length-1; i++){
        k = mas1[j, parseInt(source[i])];
        result += k;
        j++;
        if (j >= 10){
            j = 0
        }
    }

    return result
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question