Translate Java code in javascript

Hello,

is it possible to translate this snap of code into Javascript ?

The method checks wether an isin code (string) is correct or not :

      public boolean validIsin(String src) {
            int s = 0;
            int a = (src.length() == 12) ? 1 : 2;
            for (int i = src.length() - 1; i >= 0; i--) {
                  int c = src.charAt(i);
                  if (c > '9') {
                        c -= ('A' - 10);
                        s += (3 - a) * (c / 10) + a * c + (a - 1) * (c % 10) / 5;
                  } else {
                        c -= '0';
                        s += a * c + (a - 1) * (c / 5);
                        a = 3 - a;
                  }
            }
            s %= 10;
            return ((10 - s % 10) % 10) == 0;
      }

Must return true for the following strings :
US0378331005
AU0000XVGZA3
GB0002634946
Should return false if you modify some numbers in the above strings.

Thank you for any help.
LVL 9
matthew016Asked:
Who is Participating?
 
malfunction84Commented:
function validIsin(src) {
    var s = 0;
    var a = (src.length == 12) ? 1 : 2;
    for (var i = src.length - 1; i >= 0; i--) {
        var c = src.charCodeAt(i);
        if (c > '9'.charCodeAt(0)) {
            c -= ('A'.charCodeAt(0) - 10);
            s += (3 - a) * Math.floor(c / 10) + a * c + Math.floor((a - 1) * (c % 10) / 5);
        } else {
            c -= '0'.charCodeAt(0);
            s += a * c + (a - 1) * Math.floor(c / 5);
            a = 3 - a;
        }
    }
    s %= 10;
    return ((10 - s % 10) % 10) == 0;
}
0
 
malfunction84Commented:
You should probably tag this with Java as well.
0
 
matthew016Author Commented:
thanks, works great,
what do you mean tag this with java ?
0
 
malfunction84Commented:
I mean add the "Java" tag to your question so it's easier for others to find later.  Your question requires familiarity with both Java and Javascript.

It may not be possible to change the tags after the question is posted.  If that is the case, you can post a request in the Community boards for one of the admins to change the tags.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.