【问题标题】:Store coordinates from prompt and calculate the distance从提示存储坐标并计算距离
【发布时间】:2015-10-10 21:14:15
【问题描述】:

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

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));

我想稍微改进一下,并发送一个提示来存储用户发送的坐标。

示例:

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

我想存储这些坐标并用我的函数计算距离。

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

非常感谢

【问题讨论】:

    标签: javascript arrays coordinates store prompt


    【解决方案1】:
    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));
    

    【讨论】:

    • 未完全校对。假设用户输入了一个没有空格的字符串,那么您的逻辑将失败。
    【解决方案2】:

    有效且经过测试的代码。从提示中获取坐标并将其传递给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");
    

    希望这会对你有所帮助。

    【讨论】:

      【解决方案3】:

      如果你使用prompt,你不需要也用alert包装它。

      另外,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);
      }
      

      如果您想知道还能用它们做什么,我建议您阅读MDN docs for prompt()。 此外,如果您想解析文本字符串中的值,the docs for JSON.parse() 也很有用。

      【讨论】:

      • 是的,JSON.parse 需要 try-catch 逻辑。这是一个很好的做法,似乎是一个更好的答案
      • 我还不知道 JSON,我是初学者,但我很快就会学习它;)非常感谢你的帮助,它很棒
      • @Moody_jr JSON实际上非常简单,您在程序中定义的任何javascript变量都已经是有效的JSON,这意味着您可以将来自JS程序的任何数据存储为简单的JSON文本并加载它(或保存任何数据)只需一个简单的函数调用。它是当今网络非常流行的一种格式,无论如何只要学习 javascript 就可以理解它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 2020-12-26
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多