{"ScriptPreparationCode":null,"TestCases":[{"Name":"mina","Code":"const responseToTimeseries = (response) =\u003E {\r\n return response.reduce((base, { data }, index) =\u003E {\r\n data.forEach(([key, point]) =\u003E {\r\n const series = base[key];\r\n\r\n if (series) {\r\n const { points, total } = series;\r\n const pointsDiff = index - points.length;\r\n // Add enough \u0027buffer\u0027 0s to match the current index\r\n const buffer = pointsDiff ? new Array(pointsDiff).fill(0, 0, pointsDiff) : [];\r\n\r\n base[key] = {\r\n ...series,\r\n points: [...points, ...buffer, point],\r\n total: total \u002B point\r\n }\r\n } else {\r\n base[key] = {\r\n // Pad as many 0s as needed to match the current index (length)\r\n // [0, point] satifies the tests, but data can be crazy sometimes \uD83E\uDD2F\r\n // This feels a bit more reliable\r\n points: new Array(index).fill(0, 0, index).concat(point),\r\n total: point,\r\n x: key,\r\n }\r\n }\r\n })\r\n \r\n return base;\r\n }, {});\r\n};\r\n\r\n\r\nresponseToTimeseries([{\r\n data: [\r\n [1242362340000, 20]\r\n ]\r\n },\r\n {\r\n data: [\r\n [1242362340000, 10],\r\n [1242362340001, 4],\r\n [1242362340002, 1]\r\n ]\r\n },\r\n {\r\n data: [\r\n [1242362340000, 6],\r\n [1242362340001, 9]\r\n ]\r\n },\r\n {\r\n data: [\r\n [1242362340000, 11],\r\n [1242362340001, 13],\r\n [1242362340002, 2]\r\n ]\r\n },\r\n ])","IsDeferred":false},{"Name":"sina","Code":"const responseToTimeseries = (response) =\u003E {\r\n // your implementation goes here \r\n const pointHash = {}\r\n \r\n class TimeSeriesPoint {\r\n constructor(x, point) {\r\n \tthis.x = x\r\n \tthis.points = [point]\r\n }\r\n addPoint(point) {\r\n\t\tthis.points.push(point) \r\n }\r\n calcTotal() {\r\n return this.points.reduce((acc, curr) =\u003E acc \u002B curr, 0)\r\n }\r\n toJson() {\r\n \treturn {x: this.x, points: this.points, total: this.calcTotal()}\r\n }\r\n }\r\n \r\n response.flatMap(item =\u003E item.data)\r\n \t\t\t\t.forEach(([key, point]) =\u003E {\r\n \tif (pointHash[key]) {\r\n \tpointHash[key].addPoint(point)\r\n } else {\r\n \tpointHash[key] = new TimeSeriesPoint(key, point)\r\n }\r\n })\r\n \r\n const plainJsonHash = {}\r\n Object.keys(pointHash)\r\n \t\t\t.forEach(key =\u003E {\r\n \tplainJsonHash[key] = pointHash[key].toJson()\r\n })\r\n return plainJsonHash\r\n};\r\n\r\n\r\nresponseToTimeseries([{\r\n data: [\r\n [1242362340000, 20]\r\n ]\r\n },\r\n {\r\n data: [\r\n [1242362340000, 10],\r\n [1242362340001, 4],\r\n [1242362340002, 1]\r\n ]\r\n },\r\n {\r\n data: [\r\n [1242362340000, 6],\r\n [1242362340001, 9]\r\n ]\r\n },\r\n {\r\n data: [\r\n [1242362340000, 11],\r\n [1242362340001, 13],\r\n [1242362340002, 2]\r\n ]\r\n },\r\n ])","IsDeferred":false}]}