简体   繁体   中英

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

Do 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. 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;

would be different to

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

which would be different to

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

Also the parts might not be numbers so convert to numbers using 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. 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):

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

Using 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()? ):

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

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. 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`;
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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