Javascript 存储提示中的坐标并计算距离

Javascript 存储提示中的坐标并计算距离,javascript,arrays,coordinates,store,prompt,Javascript,Arrays,Coordinates,Store,Prompt,我致力于坐标之间的距离计算,我构建了一些工作正常的东西 var pointsCoordinates = [[5,7],[5,8],[2,8],[2,10]]; function lineDistance(points) { var globalDistance = 0; for(var i=0; i<points.length-1; i++) { globalDistance += Math.sqrt(Math.pow( points[i+1][0]-points[i][0

我致力于坐标之间的距离计算,我构建了一些工作正常的东西

var pointsCoordinates = [[5,7],[5,8],[2,8],[2,10]];
function lineDistance(points) {
  var globalDistance = 0;
  for(var i=0; i<points.length-1; i++) {
    globalDistance += Math.sqrt(Math.pow( points[i+1][0]-points[i][0] , 2 ) + Math.pow( points[i+1][1]-points[i][1] , 2 ));
  }
  return globalDistance;
}

console.log(lineDistance(pointsCoordinates));
我想存储这些坐标并用我的函数计算距离

我知道我必须使用push,但它不起作用,有人可以帮我写吗?我知道这很简单但是。。。我做不到

非常感谢

var userPrompt=prompt(“发送随机坐标”);//e、 g[100,2][3,45][51,6]
var userPrompt = prompt("send me random coordinates");// e.g [100,2] [3,45] [51,6]
var pointsCoordinates = parseCoordinates(userPrompt);

function parseCoordinates(unparsedCoord) {
    var arr = unparsedCoord.split(" ");
    var pair;
    for (var i = 0; i < arr.length; i++) {
        pair = arr[i];
        arr[i] = pair.substr(1, pair.length - 2).split(",")
    }
    return arr;
}

function lineDistance(points) {
    var globalDistance = 0;
    for(var i = 0; i < points.length - 1; i++) {
        globalDistance += Math.sqrt(Math.pow(points[i + 1][0] - points[i][0], 2) + Math.pow(points[i+1][1]-points[i][1], 2));
    }
    return globalDistance;
}

console.log(pointsCoordinates);
console.log(lineDistance(pointsCoordinates));
var pointsCoordinates=parseCoordinates(userPrompt); 函数解析坐标(未解析坐标){ var arr=未拆分的坐标拆分(“”); var对; 对于(变量i=0;i
工作和测试代码。从提示符获取坐标并将其传递给
lineDistance
函数,然后将传递的字符串转换为数组

function lineDistance(points) {
  var globalDistance = 0;
  var points = JSON.parse(points); // convert entered string to array
  for(var i=0; i<points.length-1; i++) {
    globalDistance += Math.sqrt(Math.pow( points[i+1][0]-points[i][0] , 2 ) + Math.pow( points[i+1][1]-points[i][1] , 2 ));
  }
  return globalDistance;
}

var pointsCoordinates = prompt("send me random coordinates in this format [,] and I will calculate the distance");
if (coordinates != null)
    console.log(lineDistance(coordinates)); //[[5,7],[5,8],[2,8],[2,10]]
else
    alert("Entered value is null");
功能线距离(点){
var全局距离=0;
var points=JSON.parse(points);//将输入的字符串转换为数组

对于(var i=0;i如果使用
prompt
,则不需要用
警报将其包装起来

另外,
prompt
将返回一个字符串值,因此您需要解析要返回的数据(除非您对字符串没有问题)

在本例中,由于您希望返回点的数组,解析可能比转换值更复杂。我建议使用
JSON.parse()
解析输入数组

在您的情况下,您可以使用此代码

var coordString = prompt("send me random coordinates in this format [,] and I will calculate the distance");

try {
    var coordinates = JSON.parse(coordString);
    // now check that coordinates are an array
    if ( coordinates.constructor === Array ) {
        // this might still fail if the input array isn't made of numbers
        // in case of error it will still be caught by catch
        console.log(lineDistance(coordinates));
    }    
} catch(error) {
    // if something goes wrong you get here
    alert('An error occurred: ' + error);
}
如果你想知道还可以用它们做些什么,我建议你阅读。
另外,如果您想解析文本字符串中的值,它也会很有用。

没有完全校对。假设用户输入的字符串没有空格,那么您的逻辑将失败。是的,JSON.parse需要尝试捕获逻辑。这是一个很好的实践,似乎是一个更好的答案我还不知道JSON,我是初学者,但我很快就会研究它;)非常感谢你的帮助awesome@Moody_jrJSON实际上非常简单,您在程序中定义的任何javascript变量都已经是有效的JSON,这意味着您可以将JS程序中的任何数据存储为简单的JSON文本并加载(或保存任何数据)只需一个简单的函数调用。这是当今网络上非常流行的格式,你只要学习javascript就可以理解它。
var coordString = prompt("send me random coordinates in this format [,] and I will calculate the distance");

try {
    var coordinates = JSON.parse(coordString);
    // now check that coordinates are an array
    if ( coordinates.constructor === Array ) {
        // this might still fail if the input array isn't made of numbers
        // in case of error it will still be caught by catch
        console.log(lineDistance(coordinates));
    }    
} catch(error) {
    // if something goes wrong you get here
    alert('An error occurred: ' + error);
}