有人可以帮我检查从地理位置计算分数的方法吗?
帮助将不胜感激
我正在计算纬度、经度的分数,但没有得到预期的结果。
请在下面查看我的代码。我不知道我做的是对还是错
这是代码片段。请运行代码并检查输出
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;
}
预期输出:
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
},
Please help me to check the above code. I am writing correctly.
繁星淼淼
相关分类