Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
am struggling to convert Java code into javascript. For that, for example, I am converting public static int primesolution into function primesolution. I do not have much idea whether I am on the right track of converting it. I am stuck with public static void main(String[] args). How to convert this function into Javascript. Any help is highly appreciated.


import java.lang.Math;
public class EvaluateDivisors {

     public static void main(String[] args) {
        try {
            long a = Long.parseLong(args[0]);
            long b = Long.parseLong(args[1]);
            int k = Integer.parseInt(args[2]);

            if (a <= 1 || b <= a) {
                error("Error: must have 1 < A < B");
            }
            if (k <= 0 || k % 2 == 0) {
                error("Error: K must be a positive odd number");
            }
            System.out.println(solution(a, b, k));
        } catch (IndexOutOfBoundsException e) {
            error("Usage: EvaluateDivisors A B K");
        } catch (NumberFormatException e) {
            error("Error: arguments must be integers");
        }
    }


    private static int solution(long a, long b, int k) {


        if (prime(k)) {
            return primeSolution(a, b, k);
        }
        int result = 0;

         for (long n = (long) Math.sqrt(a); n*n <= b; n++) {

           int divisors = 3;


            for (long m = 2; m < n && divisors <= k; m++) {
                if (n*n % m == 0) {
                    divisors += 2;
                }
            }
            if (divisors == k) {
                result++;
            }
        }
        return result;
    }


      private static int primeSolution(long a, long b, int k) {
        int result = 0;


        int n = 2;
        while (Math.pow(n, k - 1) < a) {
            n++;
        }
        while (Math.pow(n, k - 1) <= b) {
            if (prime(n++)) {
                result++;
            }
        }
        return result;
    }


    private static boolean prime(int n) {

        for (int m = 2; m <= Math.sqrt(n); m++) {
            if (n % m == 0) {
                return false;
            }
        }
        return true;
    }
    private static void error(String message) {
        System.err.println(message);
        System.exit(1);
    }

}


What I have tried:

function EvaluateDivisors {

    function main(String[] args) {
        try {
            long a = Long.parseLong(args[0]);
            long b = Long.parseLong(args[1]);
            int k = Integer.parseInt(args[2]);

            if (a <= 1 || b <= a) {
                error("Error: must have 1 < A < B");
            }
            if (k <= 0 || k % 2 == 0) {
                error("Error: K must be a positive odd number");
            }
            System.out.println(solution(a, b, k));
        } catch (IndexOutOfBoundsException e) {
            error("Usage: EvaluateDivisors A B K");
        } catch (NumberFormatException e) {
            error("Error: arguments must be integers");
        }
    }


    function solution(long a, long b, int k) {

        if (prime(k)) {
            return primeSolution(a, b, k);
        }
        int result = 0;

        for (long n = (long) Math.sqrt(a); n*n <= b; n++) {

            int divisors = 3;

            for (long m = 2; m < n && divisors <= k; m++) {
                if (n*n % m == 0) {
                    divisors += 2;
                }
            }
            if (divisors == k) {
                result++;
            }
        }
        return result;
    }

    function primeSolution(long a, long b, int k) {
      int result = 0;

       int n = 2;
      while (Math.pow(n, k - 1) < a) {
          n++;
      }
      while (Math.pow(n, k - 1) <= b) {
          if (prime(n++)) {
              result++;
          }
      }
      return result;
  }


        function prime(int n) {
        for (int m = 2; m <= Math.sqrt(n); m++) {
            if (n % m == 0) {
                return false;
            }
        }
        return true;
    }

    function error(String message) {
        console.log(message);
        System.exit(1);
    }

}
Posted
Updated 10-Dec-22 13:09pm

You don't, because it won't work.
The two languages do not operate in the same environments, and while it may be possible to "translate" the code itself, the framework around them is very, very different. For starters, the Java code you show works on command line parameters, which Javascript just doesn't have.

Instead, sit down with your homework question, and try to write your own code to do the task it sets out (which would appear to be something to do with prime factors). You will not pass your course by copy'n'paste solutions because when you get to the final exam, you won't have any access to solutions to copy'n'paste from...
 
Share this answer
 
Java and JavaScript are indeed two different programming languages, so you can't directly convert one to the other. However, many concepts and syntax in Java are similar to those in JavaScript, so you can often translate Java code into JavaScript with some effort.

To convert the code you've provided to JavaScript, you'll need to make the following changes:

Remove the import statement at the top of the file, as JavaScript does not have a concept of importing other code files.

Change the public class declaration to a JavaScript function declaration. For example, public class EvaluateDivisors can be changed to:

function EvaluateDivisors() {
    // class body goes here
}



Replace the main method with a JavaScript function that will be called when the code is run. For example, public static void main(String[] args) can be changed to:

function run(args) {
    // main method body goes here
}


Replace Java data types (e.g. int, long) with equivalent JavaScript data types (e.g. Number, String). For example, int result = 0; can be changed to let result = 0;.

Replace Java method calls (e.g. Long.parseLong) with equivalent JavaScript method calls (e.g. parseInt). For example, long a = Long.parseLong(args[0]); can be changed to let a = parseInt(args[0]);.

Convert any remaining Java syntax to JavaScript syntax. For example, for (int i = 0; i < 10; i++) can be changed to for (let i = 0; i < 10; i++).

Here is an example of what your code might look like after these changes:

function EvaluateDivisors() {

    function run(args) {
        try {
            let a = parseInt(args[0]);
            let b = parseInt(args[1]);
            let k = parseInt(args[2]);

            if (a <= 1 || b <= a) {
                error("Error: must have 1 < A < B");
            }
            if (k <= 0 || k % 2 == 0) {
                error("Error: K must be a positive odd number");
            }
            console.log(solution(a, b, k));
        } catch (e) {
            if (e instanceof IndexOutOfBoundsException) {
                error("Usage: EvaluateDivisors A B K");
            } else if (e instanceof NumberFormatException) {
                error("Error: arguments must be integers");
            }
        }
    }

    function solution(a, b, k) {
        if (prime(k)) {
            return primeSolution(a, b, k);
        }
        let result = 0;
        for (let n = Math.sqrt(a); n*n <= b; n++) {
            let divisors = 3;
            for (let m = 2; m < n && divisors <= k; m++) {
                if (n*n % m == 0) {
                    divisors += 2;
                }
            }
            if (divisors == k) {
                result++;
<pre>        }
        return result;
    }

    function primeSolution(a, b, k) {
        let result = 0;
        let n = 2;
        while (Math.pow(n, k - 1) < a) {
            n++;
        }
        while (Math.pow(n, k - 1) <= b) {
            if (prime(n++)) {
                result++;
            }
        }
        return result;
    }

    function prime(n) {
        for (let m = 2; m <= Math.sqrt(n); m++) {
            if (n % m == 0) {
                return false;
            }
        }
        return true;
    }

    function error(message) {
        console.error(message);
        process.exit(1);
    }
}



Note that this code is not guaranteed to be correct, as it is only an example of what the converted code might look like. It is always a good idea to test and debug your code to ensure that it is working as expected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900