14,426,338 members
Rate this:
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 7-Dec-19 5:20am

Rate this:

## Solution 1

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...