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

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,26 @@
+// objective:%0A
 // Read the 
@@ -74,19 +74,16 @@
  sliding
-%0A//
  window 
@@ -90,16 +90,19 @@
 size, N,
+%0A//
  as the 
@@ -140,19 +140,16 @@
 the rest
-%0A//
  will be
@@ -171,14 +171,8 @@
 ers.
-%0A//%0A//
  Ret
@@ -178,16 +178,19 @@
 turn the
+%0A//
  Moving 
@@ -241,19 +241,16 @@
  and its
-%0A//
  N-1 pre
@@ -267,16 +267,19 @@
  where N
+%0A//
  is the 
@@ -315,19 +315,16 @@
 l output
-%0A//
  should 
@@ -357,16 +357,19 @@
 g median
+%0A//
  corresp
@@ -388,19 +388,16 @@
 ch entry
-%0A//
  in the 
@@ -433,16 +433,29 @@
 ommas.%0A%0A
+// solution:%0A
 const as
@@ -473,25 +473,28 @@
 ire(
-'
+%22
 assert
-'
+%22
 );%0A%0A
-const
+function
  Mov
@@ -498,35 +498,29 @@
 MovingMedian
- = 
 (arr)
- =%3E
  %7B%0A  const N
@@ -638,17 +638,16 @@
 r.slice(
-(
 i + 1 - 
@@ -651,17 +651,16 @@
  - N %3E 0
-)
  ? i + 1
@@ -703,16 +703,15 @@
 oin(
-','
+%22,%22
 );%0A%7D
-;
 %0A%0Aco
@@ -726,21 +726,19 @@
 edian = 
-(
 arr
-)
  =%3E %7B%0A  
@@ -847,25 +847,24 @@
  / 2%5D + arr%5B
-(
 arr.length /
@@ -865,17 +865,16 @@
 ngth / 2
-)
  - 1%5D) /
@@ -932,17 +932,17 @@
 st a1 = 
-'
+%22
 5, 2, 4,
@@ -943,17 +943,17 @@
  2, 4, 6
-'
+%22
 ; // inp
@@ -970,15 +970,15 @@
 1 = 
-'
+%22
 2,3,4
-'
+%22
 ; //
@@ -1042,17 +1042,17 @@
 st a2 = 
-'
+%22
 3, 0, 0,
@@ -1067,17 +1067,17 @@
 2, 0, -2
-'
+%22
 ; // inp
@@ -1090,17 +1090,17 @@
 st r2 = 
-'
+%22
 0,0,0,0,
@@ -1108,9 +1108,9 @@
 ,0,0
-'
+%22
 ; //

add objective and solution, edit funtion