0

I started my first project and it was going well, the Javascript was a quadratic equation solver that I tested and it said "document.write can be a form of eval." how can I fix this?

This is the javascript:

function solve(a, b, c) {
  var result = (-1 * b + Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
  var result2 = (-1 * b - Math.sqrt(Math.pow(b, 2) - (4 * a * c))) / (2 * a);
  return result + "<br>" + result2;
} 
document.write(solve(1, 1, -1));
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77
Antifreeze
  • 21
  • 2
  • 4
    " ... and it said" — **what** said that? – Pointy Oct 30 '18 at 13:35
  • 1
    By not using `document.write`. – Roberto Zvjerković Oct 30 '18 at 13:38
  • To answer the "how can i fix this" portion of the question, [this post](https://stackoverflow.com/questions/15048194/why-am-i-receiving-this-jsfiddle-error-document-write-can-be-a-form-of-eval?noredirect=1&lq=1) provides an alternative. – Alexander Nied Oct 30 '18 at 13:57
  • I suggest you don't interconnect JavaScript logic with HTML rendering this way. Your function should return an array of numbers. Displaying such array should be the job of an entirely difference piece of code. – Álvaro González Oct 30 '18 at 14:07
  • Álvaro González, what is something in html that i can use to display the array? – Antifreeze Oct 30 '18 at 15:17
  • @Antifreeze Regular [DOM functions](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model). Here's [an example](http://jsfiddle.net/ureL4xh1/). Yet I have a strong feeling that you're just debugging your code, in which case you can merely use the [console](https://developer.mozilla.org/en-US/docs/Web/API/Console/log). – Álvaro González Oct 31 '18 at 09:18

0 Answers0