-3

I am trying to use JavaScript to solve the linear equation with variables.

So my try is this:

            var CE = parseFloat(document.getElementById("CE").value)
            var CF = parseFloat(document.getElementById("CF").value)
            var EF = parseFloat(document.getElementById("EF").value)
            var x1=algebra.parse("CE^2+2*EF*x-EF^2");
            var x2=algebra.parse("CF^2");
            var eq= new Equation(x1,x2);

            var h=eq.solveFor("x");

I know I should not put the valuable in "" mark, but I do not know where I should put them.

Please help me. Thank you!

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106

1 Answers1

1

You can use template String in ES-6 to simplify writing these complicated string.

var x1=algebra.parse(`${CE}^2+2*${EF}*x-${EF}^2`);
var x2=algebra.parse(`${CF}^2`);
var eq= new Equation(x1,x2);
var h=eq.solveFor("x");
Rohit Agrawal
  • 1,496
  • 9
  • 20
  • Thank you for quick response, However, it is not working. Still stops at where I have the problem. – Shunichi Ono Dec 07 '17 at 08:47
  • Can you please tell what is the problem because it is working for me – Rohit Agrawal Dec 07 '17 at 08:49
  • Hi @ShunichiOno, updated my answer. Can you please check now – Rohit Agrawal Dec 07 '17 at 08:57
  • I am sorry, but it did not work on mine. CE, CF, EF are actually user input. And those are set to numbers. The problem for now is algebra.parse is not working. – Shunichi Ono Dec 07 '17 at 09:09
  • Are you getting correct values of variables `CE`, `CF`, `EF`? If you are getting that correct please share what you are getting in `x1` and `x2` – Rohit Agrawal Dec 07 '17 at 09:14
  • CE, EF, CF are correct and used in some other function part, but they are in type of number. The input box is set to type = "number". When I try to use parseFloat($(document.getElementById("EF")).text());, it did not work to me. – Shunichi Ono Dec 07 '17 at 09:18
  • `parseFloat(document.getElementById("CE").value)` will also work on `input` fields as you were using it earliar. Please tell me where you are facing the issue? In reading these values or in algebra part? and exactly what error you are facing? – Rohit Agrawal Dec 07 '17 at 09:20
  • Right now I am having problem with algebra part. – Shunichi Ono Dec 07 '17 at 09:24
  • exactly what error you are facing? Have you included the algebra script properly? – Rohit Agrawal Dec 07 '17 at 09:25
  • I don't get the error message, so I cannot tell what exact error is. I am also not sure about whether I included the algebra script properly or not. I have downloaded it and put "src = ...". That is what I did. – Shunichi Ono Dec 07 '17 at 09:56