const assert = require('assert');

const MovingMedian = (arr) => {
  const N = arr.indexOf(0, 1);
  const r = [arr[0]];
  for (let i = 1; i < arr.length; i += 1) {
    r.push(getMedian(arr.slice((i + 1 - N > 0) ? i + 1 - N : 0, i + 1)));
  }
  return r.join(',');
};

const getMedian = (arr) => {
  arr.sort((a, b) => a - b); // ascending sort

  if (arr.length % 2 === 0) {
    return (arr[arr.length / 2] + arr[(arr.length / 2) - 1]) / 2;
  }
  return arr[(arr.length - 1) / 2];
};

const a1 = '5, 2, 4, 6'; // input
const r1 = '2,3,4'; // output
const t1 = MovingMedian(a1);
assert(r1, t1);

const a2 = '3, 0, 0, -2, 0, 2, 0, -2'; // input
const r2 = '0,0,0,0,0,0,0'; // output
const t2 = MovingMedian(a2);
assert(r2, t2);

// console.log(r2);

@@ -1,12 +1,447 @@
+// Read the array of numbers stored in arr which contains a sliding%0A// window size, N, as the first element in the array and the rest%0A// will be a list of numbers.%0A//%0A// Return the Moving Median for each element based on the element and its%0A// N-1 predecessors, where N is the sliding window size. The final output%0A// should be a string with the moving median corresponding to each entry%0A// in the original array separated by commas.%0A%0A
 const assert

add comment on solution