简体   繁体   English

JavaScript数学函数解析器

[英]Javascript math function resolver

I'm trying to make a function where you will input your variables in an HTML form and it will calculate all Y points, but for some reasons this line of code doesn't work: m * ( x * x ) + c * x + p = points; 我正在尝试创建一个函数,在该函数中您将以HTML形式输入变量,并且它将计算所有Y点,但是由于某些原因,此行代码不起作用: m * ( x * x ) + c * x + p = points; I don't know if my syntax is wrong or something? 我不知道我的语法错误还是什么? Full code is here: 完整代码在这里:

var m = document.getElementById("m").value;
var c = document.getElementById("c").value;
var p = document.getElementById("p").value;
var x = -10;
var points;
var y = -10;
var functionPoints = points[0];
function functionResolver(m, c, p) {
        while ( x > -11 && x < 11 ) {
         m * ( x * x ) + c * x + p = points;
         points[y] = points;
         y += 1;
         x += 1;
       };
     };

document.getElementById("m").value is a string . document.getElementById("m").value是一个string

Do var m = Number(document.getElementById("m").value); var m = Number(document.getElementById("m").value);

And you need a right-hand side assignment : 您需要进行右侧分配:

 points[y] = m * ( x * x ) + c * x + p;

The program will do it in order of PEMA. 该程序将按照PEMA的顺序进行操作。 You can either place extra brackets around the calculations or break the calculation down into smaller parts. 您可以在计算周围放置额外的括号,也可以将计算分解为较小的部分。

•Parentheses •Exponents •Multiplication/Division (left to right) •Addition/Subtraction (left to right). •括号•指数•乘法/除法(从左到右)•加法/减法(从左到右)。

For example 例如

(m * ( x * x )) + c * x + p = points; (m *(x * x))+ c * x + p =点;

would be different to 将与

m * ( x * x ) + (c * x) + p = points; m *(x * x)+(c * x)+ p =点;

which would be different to 这将不同于

**m * ( x * x ) + (c * (x + p)) = points; ** m *(x * x)+(c *(x + p))=点;

Also the parts might not be numbers so convert to numbers using Number() 此外,这些部分可能不是数字,因此请使用Number()转换为数字

As the other answers mentioned, you must convert the inputs from a string to a number. 正如提到的其他答案一样,您必须将输入从字符串转换为数字。 Also, always take care to put the variable name on the left hand side of the expression (yours is on the right), and do not override your points array variable (declared above the while loop) with the calculated result. 另外,请始终注意将变量名放在表达式的左侧(您的位置在右侧),并且不要用计算结果覆盖点数组变量(在while循环上方声明)。 Here I declare a local point variable to store the single calculated result. 在这里,我声明了一个局部变量来存储单个计算结果。 There are a few ways to convert your input to a string: 有几种方法可以将输入转换为字符串:

Using parseFloat (if you want to support decimal inputs): 使用parseFloat(如果要支持十进制输入):

var point = parseFloat(m * ( x * x ) + c * x + p);
points[y] = point;

Using parseInt: 使用parseInt:

var point = parseInt(m * ( x * x ) + c * x + p);
points[y] = point;

Using Number (basically the same as parseInt, see What is the difference between parseInt() and Number()? ): 使用Number(与parseInt基本相同,请参阅parseInt()和Number()有什么区别? ):

var point = Number(m * ( x * x ) + c * x + p);
points[y] = point;

In the following example, we remove need for locally-declared point variable by condensing two lines into one (you can of course use any of the above 3 string-to-number methods): 在下面的示例中,我们通过将两行压缩为一条来消除对本地声明的点变量的需要(您当然可以使用以上3种字符串到数字方法中的任何一种):

points[y] = parseFloat(m * ( x * x ) + c * x + p);

我不确定这里的points [y]是什么意思……因为y初始化为-10,而points [-10]则基本上意味着您正在尝试访问点的索引“ -10”。

To expound upon other answers, in addition to the issues with invalid right-hand assignment and converting inputs to numeric values, there's also a need to wait for the DOM to load and allow the user time to type values into the input fields. 为了阐明其他答案,除了右手分配无效和将输入转换为数值的问题外,还需要等待DOM加载并让用户有时间在输入字段中键入值。 Your current code attempts to load values instantly rather than wait for, say, a button to be clicked. 您当前的代码尝试立即加载值,而不是等待(例如)单击一个按钮。

For a full example of how to implement this, please see this gist 有关如何实现此功能的完整示例,请参见本要点

let elM, elC, elP, elR, points;
let min = -10;
let max = 10;
document.addEventListener("DOMContentLoaded",function(){
  elM = document.getElementById("m");
  elC = document.getElementById("c");
  elP = document.getElementById("p");
  elR = document.getElementById("result");
  document.getElementById("run").addEventListener("click",calculate);
});

function functionResolver(m, c, p) {
  let points = [];
  for(let x=min; x<=max; x++) {
    points.push( [x,m * ( x * x ) + c * x + p] )
  }
  return points;
}
function calculate() {
  points = functionResolver(+elM.value,+elC.value,+elP.value);
  //wipe result table, creating col headings
  elR.innerHTML = "<tr><th>x</th><th>y</th></tr>"; 
  points.forEach( coord => {
    elR.innerHTML += `<tr><td>${coord[0]}</td><td>${coord[1]}</td></tr>\n`;
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM