title: How to iterate over an array multiple times without repeating summed elements
body: <p>I am trying to solve this problem but I don't know why I can't pass all test cases. I need some help and explanation, how can I count some array (in this example: variable s) multiple times and not repeat the same elements that I already summed.</p>
  <p>Problem description:</p>
  <blockquote>
  <p>Lily has a chocolate bar that she wants to share it with Ron for his
  birthday. Each of the squares has an integer on it. She decides to
  share a contiguous segment of the bar selected such that the length of
  the segment matches Ron's birth month and the sum of the integers on
  the squares is equal to his birth day. You must determine how many
  ways she can divide the chocolate.</p>
  <p>Consider the chocolate bar as an array of squares, <code>s=[2,2,1,3,2]</code>.
  She wants to find segments summing to Ron's birth day, <code>d=4</code> with a
  length equalling his birth month, m=2. In this case, there are two
  segments meeting her criteria: <code>[2,2]</code> and <code>[1,3]</code>.</p>
  <p>Function Description</p>
  <p>Complete the birthday function in the editor below. It should return
  an integer denoting the number of ways Lily can divide the chocolate
  bar.</p>
  <p>birthday has the following parameter(s):</p>
  <p><strong>s:</strong> an array of integers, the numbers on each of the squares of
  chocolate,
  <strong>d:</strong> an integer, Ron's birth day,   <strong>m:</strong> an integer, Ron's birth month</p>
  </blockquote>
  <p>My code:</p>
  <pre><code>function birthday(s, d, m) {
      let bars = 0;
  
      if (m !== 1) {
          s.reduce((acc, val) =&gt; (acc+val) === d ? ++bars : bars)
      } else {
          bars = 1;
      }
  
      return bars;
  }
  </code></pre>
  <p>Some cases:</p>
  <ul>
  <li><p>s = [2, 5, 1, 3, 4, 4, 3, 5, 1, 1, 2, 1, 4, 1, 3, 3, 4, 2, 1]</p>
  </li>
  <li><p>d = 18</p>
  </li>
  <li><p>m = 7</p>
  </li>
  <li><p>s = [4, 5, 4, 5, 1, 2, 1, 4, 3, 2, 4, 4, 3, 5, 2, 2, 5, 4, 3, 2, 3,
  5, 2, 1, 5, 2, 3, 1, 2, 3, 3, 1, 2, 5]</p>
  </li>
  <li><p>d = 18</p>
  </li>
  <li><p>m = 6</p>
  </li>
  <li><p>s = [4, 5, 4, 2, 4, 5, 2, 3, 2, 1, 1, 5, 4]</p>
  </li>
  <li><p>d = 15</p>
  </li>
  <li><p>m = 4</p>
  </li>
  </ul>
  <p>My code works with this:</p>
  <ul>
  <li>s = [1, 2, 1, 3, 2]</li>
  <li>d = 3</li>
  <li>m = 2</li>
  </ul>
  <p>This can be found on HackerRank &gt; Practice &gt; Algorithms &gt; Implementation</p>