Question: jQuery newbie. My goal is to loop through each article and append the img to the div with the class body. The problem is its taking every image and appending to the div with the class body. Thanks!

My Script

```
jQuery('article .date').each(function() {
   jQuery(this).closest('article').find('img').after(this);

});

```

Markup

```
<article>
   <div class="date">Feb 22, 2019</div>
   <div class="body">
     <img src="imagone.png">
      Some random text
   </div>
</article>
<article>
   <div class="date">Feb 23, 2019</div>
   <div class="body">
     <img src="imagtwo.png">
      Some random text
   </div>
</article>
<article>
   <div class="date">Feb 24, 2019</div>
   <div class="body">
     <img src="imagthree.png">
      Some random text
   </div>
</article>

```

Desired markup

```
<article>
   <div class="date">Feb 22, 2019</div><img src="imagone.png">
   <div class="body">

      Some random text
   </div>
</article>
<article>
   <div class="date">Feb 23, 2019</div><img src="imagtwo.png">
   <div class="body">

      Some random text
   </div>
</article>
<article>
   <div class="date">Feb 24, 2019</div><img src="imagthree.png">
   <div class="body">

      Some random text
   </div>
</article>

```

Answer: 
You could slice a given array and take only five elements for getting an average.

```js
function standardDeviation(array) {
    const arrAvg = tempArray => tempArray.reduce((a, b) => a + b, 0) / tempArray.length;
    return array.map((_, i, a) => arrAvg(a.slice(i, i + 5)));
}

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(standardDeviation(arr));
```
You could try using the slice() function of array element.

```
// simulated loop index
var curr_index_pos = 3; 
// entire array
var array_full = [1,2,3,4,5,6,7,8,9,10]; 

// array with 5 next values from "curr_index_pos"
var array_5 = array_full.slice(curr_index_pos,curr_index_pos+5); 

var sum = 0;
for( var i = 0; i < array_5.length; i++ ) {
   sum += parseInt( array_5[i] ); 
}

var avg = sum/array_5.length;

console.log("array_5", array_5);
// [4,5,6,7,8]
console.log("sum", sum);
// 30
console.log("avg", avg);
// 6

```