title: creating function within function - method "buy" is not defined
body: <p>I get the error method &quot;buy is not defined.&quot;. What is wrong in the below code? Please can you let me know where I am going wrong?</p>
  <pre><code>var Portfolio = function() {
  
    let stockHoldings = new Map();
    
     function buy(trade) {
      let tradesForTicker = stockHoldings.get(trade.ticker);
      if(tradesForTicker == null){
        stockHoldings.set(trade.ticker,[trade]);
      }else{
        tradesForTicker.push(trade);
      }
    }
  
     function sell(trade) {
        let ticker = trade.ticker;  
        let tradesForTicker = stockHoldings.get(ticker);
        let precision = 5;
        if(tradesForTicker != null){
            let quantityToSell = Number(Number(trade.quantity).toFixed(precision));
            while(quantityToSell &gt; 0) {
              quantityToSell = Number(Number(quantityToSell).toFixed(precision));          
              if(tradesForTicker.length &gt; 0){
                let nextTradeToSell =  tradesForTicker[0];
  
                if(nextTradeToSell.quantity == quantityToSell){
                  quantityToSell = 0;
                  tradesForTicker.splice(0,1);
                } 
                else if (nextTradeToSell.quantity &lt; quantityToSell){
                    quantityToSell = quantityToSell - nextTradeToSell.quantity;
                    tradesForTicker.splice(0,1);
                }else {
                  nextTradeToSell.quantity = nextTradeToSell.quantity - quantityToSell;
                  quantityToSell = 0;
                }
              }
            }
            if(tradesForTicker.length == 0){
              stockHoldings.delete(ticker)
            }
        }
    }
  
    this.stockHoldings = function() {
      return stockHoldings();
    }
  };
  
  function generetePortfolio(tickers, actions, quantities, prices){
    let portfolio = new Portfolio();
    for(var i=0;i&lt; tickers.length; i++){
      let ticker = tickers[i].toString();
      let action = actions[i].toString();
      let quantity = quantities[i].toString();
      let price = prices[i].toString();
  
      let transactionTrade = new Trade(ticker,quantity,price,action);
  
      if(transactionTrade.isBuyOrDrip()){
          portfolio.buy(transactionTrade);
      }
      if(transactionTrade.isSell()){
        portfolio.sell(transactionTrade);
      }
    }
    return portfolio();
  }
  </code></pre>
  <p>Further EDIT:</p>
  <p>//Added method to generate total qty and avg price, BUT THIS DOES NOT WORK</p>
  <pre><code>function myPositions(tickers, actions, quantities, prices){
    let portfolio = generetePortfolio(tickers, actions, quantities, prices);  
    let returnArray = [];
    portfolio.stockHoldings.forEach((value, key) =&gt; {  
        let totalQuantity = 0;
        let avgPrice = 0;
        let totalCost = 0;
        value.map( trade =&gt; {
          totalQuantity += trade.quantity ;
          totalCost += trade.quantity * trade.price;
        });
  
        avgPrice = totalCost/totalQuantity;      
        returnArray.push([key, totalQuantity,avgPrice]);
    } )
    return returnArray;
  }
  </code></pre>