我想验证INN代码.我在Java中有一个代码片段,它验证了INN代码,并希望将其移植到JavaScript中,以便在我的网站上使用它(React):

for (int index = 0; index < lastCharacterIndex; index++) {
     sum += Character.digit(characters.get(index)) * INN_MULTIPLIERS[index];
}

checkDigit = (sum % 11) % 10;
returnValue = Character.digit(characters.get(lastCharacterIndex), 10) == checkDigit;
private static final int[] INN_MULTIPLIERS = {-1, 5, 7, 9, 4, 6, 10, 5, 7};

我试着翻译它,但失败了:

const validateINNCode = (innCode) => {
    let sum = 0;
    const INN_MULTIPLIERS = [-1, 5, 7, 9, 4, 6, 10, 5, 7];
    for (let index = 0; index < innCode.length; index++) {
         sum += innCode[index] * INN_MULTIPLIERS[index];
    }
    const checkDigit = (sum % 11) % 10;
    const returnValue = innCode.length == checkDigit;
    return returnValue;
};

您知道如何将这段Java代码正确地移植到JavaScript中吗?谢谢.

推荐答案

将代码从Java移植到JavaScript是一项相当容易的任务.

您可能应该包含完整的Java代码.以下是我认为它会是什么样子:

我看到characters.get被称为characters.get,所以看起来charactersList<Character>.更好的做法是展示Java代码如何将String转换为List.

import java.util.List;

public class Validator {
  private static final int[] INN_MULTIPLIERS = {-1, 5, 7, 9, 4, 6, 10, 5, 7};

  public static boolean isValid(List<Character> characters) {
    int sum = 0;
    int lastCharacterIndex = characters.size() - 1;
    for (int index = 0; index < lastCharacterIndex; index++) {
        sum += Character.digit(characters.get(index), 10) * INN_MULTIPLIERS[index];
    }
    int checkDigit = (sum % 11) % 10;
    return Character.digit(characters.get(lastCharacterIndex), 10) == checkDigit;
  }

  public static void main(String[] args) {
    // for (int i = 10000000; i < 99999999; i++) {
    //     String code = Integer.toString(i, 10);
    //     List<Character> digits = code.chars().mapToObj(e -> (char) e).toList();
    //     if (isValid(digits)) {
    //         System.out.println(i); // A valid code
    //     }
    // }

    String code = "18158984"; // Should be valid
    List<Character> digits = code.chars().mapToObj(e -> (char) e).toList();
    System.out.println(isValid(digits));
  }
}

以下是等效的JavaScript代码:

  1. Character.digit可以移植到parseInt
  2. Java中的==(双等号)在JavaScript中应该是===(三个等号

const INN_MULTIPLIERS = [-1, 5, 7, 9, 4, 6, 10, 5, 7];

function isValid(characters) {
  let sum = 0;
  const lastCharacterIndex = characters.length - 1;
  for (let index = 0; index < lastCharacterIndex; index++) {
    sum += parseInt(characters[index], 10) * INN_MULTIPLIERS[index];
  }
  const checkDigit = (sum % 11) % 10;
  return parseInt(characters[lastCharacterIndex], 10) === checkDigit;
}

const code = "18158984"; // Should be valid
const digits = code.split('');
console.log(isValid(digits));

Javascript相关问答推荐

警告!合同执行期间遇到错误[执行已恢复](Base Layer 2)

node TS:JWT令牌签名以验证客户端和后台问题之间的身份验证

fs. writeFile()vs fs.writeFile()vs fs.appendFile()

如何在Angular中插入动态组件

当试图显示小部件时,使用者会出现JavaScript错误.

CheckBox作为Vue3中的一个组件

你怎么看啦啦队的回应?

显示图—如何在图例项上添加删除线效果?

阿波罗返回的数据错误,但在网络判断器中是正确的

如何解决useState错误—setSelect Image不是函数''

从页面到应用程序(NextJS):REST.STATUS不是一个函数

变量在导入到Vite中的另一个js文件时成为常量.

在SHINY R中的嵌套模块中,不能使用Java代码

JS Animate()方法未按预期工作

当从其他文件创建类实例时,为什么工作线程不工作?

我不知道如何纠正这一点.

在JavaScript中将Base64转换为JSON

为什么我的Navbar.css没有显示在本地主机页面上?

需要RTK-在ReactJS中查询多个组件的Mutations 数据

在HTML中使用meta标记来指定定制元数据以用于使用JavaScript进行检索是不是一个坏主意?