Post History
JavaScript (Node.js), 41 40 bytes Saved 1 byte thanks to Shaggy x=>x.map(e=>e?a.push(e):a.pop(),a=[])&&a Try it online! x => //x is the input x.map(e=> //For every...
Answer
#2: Post edited
# [JavaScript (Node.js)], 41 bytes- <!-- language-all: lang-javascript -->
x=>(a=[],x.map(e=>e?a.push(e):a.pop()),a)[Try it online!][TIO-kpbjv5ub]- [JavaScript (Node.js)]: https://nodejs.org
[TIO-kpbjv5ub]: https://tio.run/##bYzdCsIwDEbvfZIEYqnO4Q9kPsjYRZidTmZb1il7@1qKIqicm3DOR67ykNCOvZ@W1p1MjB3PXIFw3dCsbuLBcGWOovw9XMDgIV3OAyIJRj/2duLW2eAGowZ3XmQDHdSa3pS0bRA/ZUXrZIsvVyZ0Lpp2P/uCNrns/3x6kUp8Ag "JavaScript (Node.js) – Try It Online"- ```
x => ( //x is the inputa=[], //The accumulator to be returned (initially empty)- x.map(e=> //For every element e in x
e? //If e is not 0- a.push(e) //Add it to the accumulator
:a.pop()), //Otherwise, pop the last elementa) //Return the accumulator- ```
- # [JavaScript (Node.js)], <s>41</s> 40 bytes
- Saved 1 byte thanks to Shaggy
- <!-- language-all: lang-javascript -->
- x=>x.map(e=>e?a.push(e):a.pop(),a=[])&&a
- [Try it online!][TIO-kq8oe3ra]
- [JavaScript (Node.js)]: https://nodejs.org
- [TIO-kq8oe3ra]: https://tio.run/##y0osSyxOLsosKNHNy09J/Z9m@7/C1q5CLzexQCPV1i7VPlGvoLQ4QyNV0wrIyi/Q0NRJtI2O1VRTS/xfUJSZV2KbnJ9XnJ@TqpeTn84FFtFI04g20IFBUx3zWE1NhIyhjhFQ1BhNzBQIDcAyBjoWGOqNdUzAMpZYTIJCoMx/AA "JavaScript (Node.js) – Try It Online"
- ```
- x => //x is the input
- x.map(e=> //For every element e in x
- e? //If e is not 0
- a.push(e) //Add it to the accumulator
- :a.pop(), //Otherwise, pop the last element
- a=[]) //Initial value of the accumulator
- &&a //Return the accumulator
- ```
#1: Initial revision
# [JavaScript (Node.js)], 41 bytes <!-- language-all: lang-javascript --> x=>(a=[],x.map(e=>e?a.push(e):a.pop()),a) [Try it online!][TIO-kpbjv5ub] [JavaScript (Node.js)]: https://nodejs.org [TIO-kpbjv5ub]: https://tio.run/##bYzdCsIwDEbvfZIEYqnO4Q9kPsjYRZidTmZb1il7@1qKIqicm3DOR67ykNCOvZ@W1p1MjB3PXIFw3dCsbuLBcGWOovw9XMDgIV3OAyIJRj/2duLW2eAGowZ3XmQDHdSa3pS0bRA/ZUXrZIsvVyZ0Lpp2P/uCNrns/3x6kUp8Ag "JavaScript (Node.js) – Try It Online" ``` x => ( //x is the input a=[], //The accumulator to be returned (initially empty) x.map(e=> //For every element e in x e? //If e is not 0 a.push(e) //Add it to the accumulator :a.pop()), //Otherwise, pop the last element a) //Return the accumulator ```