Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Translate Java code in javascript

Posted on 2007-10-12
4
Medium Priority
?
690 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:matthew016
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
malfunction84 earned 2000 total points
ID: 20064931
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
 
LVL 8

Expert Comment

by:malfunction84
ID: 20064944
You should probably tag this with Java as well.
0
 
LVL 9

Author Comment

by:matthew016
ID: 20064962
thanks, works great,
what do you mean tag this with java ?
0
 
LVL 8

Expert Comment

by:malfunction84
ID: 20064992
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question