How to get data re-filtered everytime slider event runs?

I want to reset the data everytime my slider event runs but I seem to be going in a loop. This is the sequence of events I am TRYING to create:

  1. Data and chart loads up with all data, initially.
  2. Slider runs and changes the beg and end dates. I capture those dates and trigger a filterData() event.
  3. The filterData() event runs with new dates (from slider event) and should filter original data with new date parameters. This then triggers updateChart() event which takes updated filtered data and displays new chart.

I tried a bunch of things. Right now, the problem is that it does not recognize the variable data even though I made it global (which I’m not even confident is the right thing to do).

How and where can I filter data to reset it everytime slider runs?

My code: