从数组中删除“)”时遇到问题

你好,我还是编程新手,请耐心等待。我目前正在尝试制作一个程序,将(x1,y1)(x2,y2)形式的坐标作为字符串放入数组,并将字符串转换为数字,然后将所有转换后的字符串放入另一个数组。我可以对负数进行排序和换算,也可以对正数进行换算。但是我无法从数组中获取“(”和“)”吗?我试图检查未定义或NaN值,但总是将其添加到最终数组中?

<!DOCTYPE html>
<html>
<head>
	<title>Graphing Tool</title>
</head>
<body>
<p>Input your set of cordinates,no space inbetween,and select form (Example: (X1,Y1)(X2,Y2) ) </p>
<input id="inital-form" type="text" name="inital-form">
<br>
<br>
<input id="Slope-intercept-form" type="button" name="intialForm" value="Slope Intercept Form" onclick="retriveInput()">
<script type="text/javascript" src="./script.js"></script>
</body>
</html>

let convertedCords = [];

//recieving users cords
function retriveInput() {
  let input = document.getElementById("inital-form").value;
  console.log(input);

  convertCords(input);
}

//converting string of cords into numbers
function convertCords(cordsString) {
  let stringArray = cordsString.split("");

  for (let i = 0; i < stringArray.length; i++) {
    if (stringArray[i] == "-") {
      let negativeNum = parseInt(-stringArray[i + 1]);

      //removes the "-" and also removes the number I converted to a negative
      stringArray.splice(i,2);
      convertedCords.push(negativeNum);
      console.log(convertedCords);
    } else if (stringArray[i] != NaN || stringArray[i] != undefind || stringArray[i] == "(" || stringArray[i] == ")") {
      let notNegativeNum = parseInt(stringArray[i]);
      convertedCords.push(notNegativeNum);
      console.log(convertedCords);
    }
  }
}
amzonpb365 回答:从数组中删除“)”时遇到问题

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2496606.html

大家都在问