title: How to return a string as JS code that can be used to graph points?
body: <p>I am generating and graphing the Fourier series of a square wave by creating a string that contains the information needed to graph the Fourier series. I need the string to be returned as usable code (<code>Math.sin()</code>, <code>Math.PI</code>, etc) so that I can use it in my graphing function. What is the best way to do this?</p>
  <p>Here is my code:</p>
  <pre><code>//draws point to canvas
  function drawPoint(x, y) {
    ctx.fillRect(x * 20, -y * 20, 1, -1);
  }
  
  // generates function to be graphed
  function writeFunc(n) {
    
    // initialize vars
    func = &quot;&quot;;
    var i = 1;
    var x = n;
  
    // generate string
    do {
      func += &quot;(4*Math.sin(&quot;;
      func += x;
      func += &quot;*x)/&quot;;
      func += x;
      func += &quot;*Math.PI)+&quot;;
      i += 2;
      x -= 2;
    } while (i &lt; n);
  
  
    // remove final character (+) from string
    func = func.substring(0, func.length - 1);
  
    return eval(new String(func));
  }
  
  // call drawPoint() for every x value in canvas
  function graph() {
    for (var x = -200; x &lt; 200; x += 0.01) {
      drawPoint(x, writeFunc(getSliderValue()));
    }
  
    // prints string to console
    console.log(writeFunc(getSliderValue()));
  }
  </code></pre>