[JavaScript] Elegant solution using an array and pointer (with explanation)
This is a rather simple problem, all we need to do is to store all the data that was passed to insert() function, return some data if the condition is true, and
This is a rather simple problem, all we need to do is to store all the data that was passed to `insert()` function, return some data if the condition is true, and keep track of where we were last time.\n\nLook how clean this solution is:\n```javascript\nclass OrderedStream {\n  constructor(n) {\n    this.pointer = 0\n    this.list = []\n  }\n\n  insert(id, value) {\n    let chunk = []\n    this.list[id - 1] = value\n    while(this.list[this.pointer]) {\n      chunk.push(this.list[this.pointer])\n      this.pointer++\n    }\n    return chunk\n  }\n}\n```\n\nWith explanation:\n```javascript\nclass OrderedStream {\n  // Define a construction function and set some values as object properties to keep our data persistent between invocations\n  constructor(n) {\n    this.pointer = 0\n    // this will create an array of length (n) and set all values to \'undefined\'\n    this.list = []\n  }\n\n  insert(id, value) {\n    // will be used to store values that pass the condition and to be returned\n    let chunk = []\n    // since array indices start from zero and id in this problem from 1 we need to decrement it\n    this.list[id - 1] = value\n    // every time we insert a value we have to look if there is a value at the index (pointer) that should be returned\n    // if there is any we copy it and then iterate to the next element until the condition is no longer true\n    while(this.list[this.pointer]) {\n      chunk.push(this.list[this.pointer])\n      this.pointer++\n    }\n    return chunk\n  }\n}\n```
['JavaScript']