title: Memoize function passes function and returns function JavaScript
body: <p>I am having multiple problems with this function. It's part of a bonus question for a Data Structures and Algorithms class and I've invested so much time in this one problem, that I'd really like to get it to work and understand what's going on.</p>
  
  <p>There is one main problem, which has caused several little ones...this problem's name is JavaScript. We've never programmed in JavaScript before, but for some reason we have to use it.</p>
  
  <p>The function has to pass tests (this one and fibonacci), which are structured like this:</p>
  
  <pre><code>var fn = (n) =&gt; 2 * n
  var m_fn = memoize(fn)
  expect(m_fn(18)).to.equal(fn(18))
  </code></pre>
  
  <p>So I have to pass the function I want to memoize as a parameter of the memoize function and the memoize function has to return a function. I am not allowed to do it any other way.  </p>
  
  <p>I did all of the reading and researched the memoize function, but all of the implementations take a different approach.</p>
  
  <p>Basically, I understand what I have to do, but I don't quite understand HOW. I know what the memoize function should do, but I don't understand how to adjust the original function using my memoize function. This is what I have so far/what I don't have:</p>
  
  <p>I know it's wrong. But I think I'm missing something major. I am supposed to return a function, but I am returning values...</p>
  
  <p>In the test, it's writen var m_fn = memoize(fn), so the memoize function passes fn, then returns a new function, but in my memoize, I am returning values for fn(n), so I AM doing something wrong...</p>
  
  <pre><code>/**
  * Creates a memoized version of the function fn. It is assumed that fn is a referentially transparent
  * function.
  * @param {function} fn Some referentially transparent function that takes a basic datatype (i.e. number / string)
  * @returns {function} A new function that is the memoized version of fn. It never calculates the result of
  * a function twice.
  */
  memoize: (fn) =&gt; { //here we enter the function that we want to memoize
   var memory = []; //we need to create an array to hold the previously calculated values, length n (parameter of fn)
  
   if(this.n &gt; memory.length){ //Check to see if this particular value is in the array already.  
     return memory[this.n]; //How do I access the integer parameter that was passed through fn though? Is this correct?
   } else{ // if not, we want to save it and return it
     var result = fn(this.n);
     memory.push(result);
     return result;
   } 
  </code></pre>
  
  <p>}</p>