Can someone help me to check the way to calculate a score from geolocation?
Help will be appreciated
I am calculating the score from latitude, longitude but not getting the expected result.
Please see my code below. I don't know I am doing right or wrong
Here is the snippet of code. Please run the code and check the output
const suggestions = `{
suggestions: [
{
name: "London, ON, Canada",
latitude: 42.98339,
longitude: -81.23304
},
{
name: "Londontowne, MD, USA",
latitude: 38.93345,
longitude: -76.54941
},
{
name: "London, OH, USA",
latitude: 39.88645,
longitude: -83.44825
},
{
name: "Londonderry, NH, USA",
latitude: 42.86509,
longitude: -71.37395
},
{
name: "New London, CT, USA",
latitude: 41.35565,
longitude: -72.09952
},
{
name: "New London, WI, USA",
latitude: 44.39276,
longitude: -88.73983
},
{
name: "London, KY, USA",
latitude: 37.12898,
longitude: -84.08326
}
]
}`;
console.log(calculateScore(suggestions, 43.70011, -79.4163))
function calculateScore(suggestions, latitude, longitude) {
const suggestionsResult = suggestions;
for (let i = 0; i < suggestionsResult.length; i += 1) {
let score = 0;
const lat = Math.abs(suggestionsResult[i].latitude - latitude);
const long = Math.abs(suggestionsResult[i].longitude - longitude);
score = 10 - (lat + long) / 2;
score = score > 0 ? Math.round(score) / 10 : 0;
suggestionsResult[i].score = score;
}
return suggestionsResult;
}
Expected output :
suggestions: [{
name: "London, ON, Canada",
latitude: 42.98339,
longitude: -81.23304,
score: 0.9
},
{
name: "Londontowne, MD, USA",
latitude: 38.93345,
longitude: -76.54941,
score: 0.5
},
{
name: "London, OH, USA",
latitude: 39.88645,
longitude: -83.44825,
score: 0.5
},
{
name: "Londonderry, NH, USA",
latitude: 42.86509,
longitude: -71.37395,
score: 0.5
},
{
name: "New London, CT, USA",
latitude: 41.35565,
longitude: -72.09952,
score: 0.5
},
{
name: "New London, WI, USA",
latitude: 44.39276,
longitude: -88.73983,
score: 0.5
},
{
name: "London, KY, USA",
latitude: 37.12898,
longitude: -84.08326,
score: 0.4
}
]
}```
Please help me to check the above code. I am writing correctly.