To achieve Java code that closely resembles JavaScript code, disregarding undefined and null values, it is best to assume that the input is an Object[], as indicated by the numerous Number() calls.
public static double arrayMax(Object... arr) {
double rangomax = 99999;
double max = Double.NEGATIVE_INFINITY;
for (Object obj : arr) {
double value = number(obj);
if (value > max && value < rangomax) {
max = value;
}
}
return max;
}
// Function similar to JavaScript's Number()
private static double number(Object obj) {
if (obj instanceof Number) {
return ((Number) obj).doubleValue();
}
try {
return Double.parseDouble(obj.toString());
} catch (NumberFormatException e) {
return Double.NaN;
}
}
You may also consider creating overloads for primitive values, such as:
public static double arrayMax(double... arr) {
double rangomax = 99999;
double max = Double.NEGATIVE_INFINITY;
for (double value : arr) {
if (value > max && value < rangomax) {
max = value;
}
}
return max;
}