title: How to refer to previous items in array dynamically, without using "i-1", "i-2", etc.?
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function play_music(song) {
    let played_notes = [];
    let secs = [];
    let position = [];
    let secs_to_wait = 0;
    let time_elapsed = 0;
  
    console.log("Song started.")
  
    for (let key in song) {
      played_notes.push(song[key].note);
      secs.push(song[key].lasts);
      position.push(song[key].starts_at);
    }
  
    for (let i in position) {
      if (position[i] &gt; position[i - 1]) {
        secs_to_wait = position[i] - secs_to_wait;
        console.log("Wait " + secs_to_wait + " second(s).");
        console.log("Play " + played_notes[i]);
      } else {
        console.log("Play " + played_notes[i]);
      }
  
      if (position[i] == position[i - 1] &amp;&amp; secs[i] == secs[i - 1] || position[i] + secs[i] == secs[i - 1]) {
        //do nothing.
      } else {
        time_elapsed += secs[i];
      }
  
      if ((secs[i] + position[i]) &lt; time_elapsed || secs[i] &lt; time_elapsed) {
        console.log(played_notes[i] + " released.")
      }
    }
    return "Song ends.";
  }
  
  let my_song = [{
      note: 'C',
      starts_at: 0,
      lasts: 4
    },
    {
      note: 'E',
      starts_at: 0,
      lasts: 4
    },
    {
      note: 'G',
      starts_at: 2,
      lasts: 2
    },
    {
      note: 'G#',
      starts_at: 3,
      lasts: 2
    },
    {
      note: 'Eb',
      starts_at: 3,
      lasts: 2
    }
  ];
  
  console.log(play_music(my_song))</code></pre>
  </div>
  </div>
  </p>
  <p>In this program I am supposed to make a function which returns console messages of musical notes from an array of objects chronologically, when they start, how much time to wait between them, and when to release those notes. I have three elements in each object: the musical note name, starting position in seconds, and how long it lasts (also in seconds).</p>
  <p>My code works 90% when I go through each element by index, but when I get to return &quot;release&quot; messages on the console, I can't refer to musical notes that come before the index I'm in. And if I just put the following, it will only work for the current order of elements, it won't work dynamically, regardless of order:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>  if ((secs[i] + position[i]) &lt; time_elapsed || secs[i] &lt; time_elapsed){
       console.log((played_notes[i] + played_notes[i-1] + played_notes[i-2]) + " released.")
    }</code></pre>
  </div>
  </div>
  </p>
  <p>For now if you run the entire code shown previously, only &quot;G&quot; will be released, &quot;C&quot; and &quot;E&quot; don't get released. And later, &quot;G#&quot; and &quot;Eb&quot; don't get released simultaneously, but rather &quot;G#&quot; plays and gets released, and just later &quot;Eb&quot; plays and gets released.</p>
  <p>Not to mention that I don't get the &quot;Wait x second(s).&quot; message before &quot;G#&quot; and &quot;Eb&quot; are played.</p>
  <p>So something is not working, and the console output I'm expecting to get at the end is like this:</p>
  <pre><code>Song started.
  Play C
  Play E
  Wait 2 second(s).
  Play G
  Wait 1 second(s).
  Play G#   
  Play Eb
  C released.
  E released.
  G released.
  Wait 1 second(s).
  G# released.
  Eb released.
  Song ends.
  </code></pre>
  <p>NOTE: I am a beginner, and for this task I am not allowed to use anything more advanced than for loops, while loops, if statements, array and object built-in functions. So foreach or anything beyond that won't cut it. Please try to keep it as simple as possible in this sense, it may require a longer answer.</p>