title: how to iterate in array and returnn new array having sum of items?
body: <p>I have one question in my exam I want to know how I will implement this in JavaScript.</p>
  
  <p><strong>Question</strong>
  During the war, the enemy battalion has planted a bomb in your bunker. Your informer has sent you a message of the enemy which contains a list having N numbers and a key. The numbers are used to construct a sequence to defuse the bomb. According to your informer, the logic to extract the sequence from the whole message is by replacing each number with the sum of the next key numbers. When the value of key is negative, the number is replaced by the sum of the previous key numbers. The series of numbers is considered in a cyclic fashion for the last key numbers.</p>
  
  <blockquote>
    <p>The input to the function/method consists of three arguments </p>
    
    <p>size: an integer representing the size of the list (N).<br>
    key: an integer representing the key.<br>
    message: representing the list of integers. </p>
    
    <p>output:<br>
    Return a list of integers representing the sequence to defuse
    the bomb.</p>
    
    <p>input:<br>
    message= [4,2,-5,11]<br>
    size =4<br>
    key =3  </p>
    
    <p>steps [2,-5,11] ==> 8<br>
    steps[-5,11,4]==>10<br>
    steps[11,4,2]==>17<br>
    steps[4,2,-5]==>1   </p>
    
    <p>expected output =[8,10,17,1]</p>
  </blockquote>
  
  <pre><code>function abc(size, key, arr) {
    var newArr = []
    for (var i = 0; i &lt; size; i++) {
      var tem = 0;
      if (tem &lt; key) {
        tem++
        sum = arr[tem]
      }
  
      newArr.push(sum)
    }
  </code></pre>
  
  <p>here is my code
  <a href="https://jsfiddle.net/khh5sqyh/1/" rel="nofollow noreferrer">https://jsfiddle.net/khh5sqyh/1/</a></p>
  
  <p>any update ?? can we make this in javascript</p>
  
  <p>got the answer like that</p>
  
  <pre><code>var ms = [4, 2, -5, 11];
  var size = 4,
    key = 3;
  
  var newArr=[];
  for (var i = 0; i &lt; ms.length; i++) {
      sum=0;
      for(var j=1 ;j&lt;=key ;j++ ){
        sum += ms[(i+j)%size];
    }
    newArr.push(sum);
  }
  
  console.log(newArr)
  </code></pre>