I want to validate the INN code. I have code snippet in Java which validates the INN code and want to port it to JavaScript to use it on my website (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};
I tried to translate it but it fails:
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;
};
Any ideas how to correctly port this Java code to JavaScript? Thanks.
Porting code over from Java to JavaScript is a fairly easy task.
You should probably include the full Java code. Here is what I believe it would look like:
Note: I see that
characters.getis called, so it looks likecharactersis aList<Character>. It would have been better to show how the Java code translated aStringinto thisList.Here is the equivalent JavaScript code:
Character.digitcan be ported over toparseInt==(double equals) in Java should be===(triple equals) in JavaScript