Hello.
I have solved this puzzle both in JS and Java.
For Java i got 100% validation with 3rd test case being wrong, but that’s ok.
And my JS solution is not passing though the math looks the same…
I was wondering if anyone can help me spot an error.
I know that in Java you have to use doubles as the Math functions take doubles in when calculating.
In Js you dont have that option and only are left to parse to float.
Here is my Java to JS comparison:
private static double getDistance(double longitudeA,double longitudeB,double latitudeA,double latitudeB){
Double x= (longitudeB-longitudeA)*Math.cos((latitudeA+latitudeB)/2);
Double y=latitudeB-latitudeA;
Double dist =Math.sqrt(Math.pow(x, 2)+Math.pow(y,2))*6371;
return dist;
}
function getDist(cLon,cLat,gLon,gLat){
const x =(gLon-cLon)*Math.cos((cLat+gLat)/2);
const y =gLat-cLat;
return Math.sqrt(x*x+y*y)*6371;
}
Transforming degrees to radians:
Double LON = Math.toRadians(Double.parseDouble(in.next().replace(",",".")));
Double LAT = Math.toRadians(Double.parseDouble(in.next().replace(",",".")))
//for array of defibs
Double longitude=Math.toRadians(Double.parseDouble(
DEFIB.split(";")[DEFIB.split(";").length-2].replace(",", ".")
));
Double latitude=Math.toRadians(Double.parseDouble(
DEFIB.split(";")[DEFIB.split(";").length-1].replace(",", ".")
));
const LON = parseFloat(readline().replace(',','.'))*(Math.PI/180);
const LAT = parseFloat(readline().replace(',','.'))*(Math.PI/180);
//for array of defigs
parseFloat(DEFIB.split(";").slice(-1).reverse().pop().replace(',','.'))*(Math.PI/180),
parseFloat(DEFIB.split(";").pop().replace(',','.'))*(Math.PI/180)