Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

71%
+3 −0
Challenges When The Ternary Is Balance

JavaScript (Node.js), 47 40 50 49 bytes -1 (possibly -3) bytes thanks to Shaggy! f=(n,s='0')=>n?f(n/3+n%3/2|0,'')+"+-0+-"[n%3+2]:s Try it online! A basic recursive solution.

posted 3y ago by Moshi‭  ·  edited 3y ago by Moshi‭

Answer
#5: Post edited by user avatar Moshi‭ · 2021-09-10T06:57:55Z (over 3 years ago)
  • # [JavaScript (Node.js)], ~~47~~ ~~40~~ 50 bytes
  • *-7 bytes thanks to Shaggy!*
  • *Why is fixing the zero case so expensive*
  • ```
  • f=(n,s='0')=>n?f(n/3+n%3/2|0,'')+"+-0+-"[n%3+2]:s
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcjBCoIwGADgcz7FjxTb@J1OhQ7a6iE6RuBQFwvZxI0u5rMvodvH91Yf5fvFzIFbN4wxaklt5iURhMmrvWlqixrtqS6qr8gIYZgiF8jTx35YPRsftVvoNAYwIIGXQrS7LvAHIoM1OfTOejeN@eRetDuuZmugy2c13INaAj0zQNDUMNYmW/wB "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], ~~47~~ ~~40~~ ~~50~~ 49 bytes
  • *-1 (possibly -3) bytes thanks to Shaggy!*
  • ```
  • f=(n,s='0')=>n?f(n/3+n%3/2|0,'')+"+-0+-"[n%3+2]:s
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcjBCoIwGADgcz7FjxTb@J1OhQ7a6iE6RuBQFwvZxI0u5rMvodvH91Yf5fvFzIFbN4wxaklt5iURhMmrvWlqixrtqS6qr8gIYZgiF8jTx35YPRsftVvoNAYwIIGXQrS7LvAHIoM1OfTOejeN@eRetDuuZmugy2c13INaAj0zQNDUMNYmW/wB "JavaScript (Node.js) – Try It Online"
#4: Post edited by user avatar Moshi‭ · 2021-09-10T06:57:06Z (over 3 years ago)
  • # [JavaScript (Node.js)], ~~47~~ ~~40~~ 50 bytes
  • *-7 bytes thanks to Shaggy!*
  • *Why is fixing the zero case so expensive*
  • ```
  • f=(n,s='0')=>n?f(n/3+(n%=3)/2|0,'')+"+-0+-"[n+2]:s
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcZBDoIwEADAs7xiQ2K6m6VS4QZWH2I8EKSmpNkaSryob68knmbm4TWkcfHPVUu8Tzk7i1Ilq4wie5aLQ6lbRtnblurmYyqliEvWhnV5FW5uXcouLhimFTxY0Edj@m0n@IeZ4F3sxigphukQ4gM9MKgO1IZDT9QX3/wD "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], ~~47~~ ~~40~~ 50 bytes
  • *-7 bytes thanks to Shaggy!*
  • *Why is fixing the zero case so expensive*
  • ```
  • f=(n,s='0')=>n?f(n/3+n%3/2|0,'')+"+-0+-"[n%3+2]:s
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcjBCoIwGADgcz7FjxTb@J1OhQ7a6iE6RuBQFwvZxI0u5rMvodvH91Yf5fvFzIFbN4wxaklt5iURhMmrvWlqixrtqS6qr8gIYZgiF8jTx35YPRsftVvoNAYwIIGXQrS7LvAHIoM1OfTOejeN@eRetDuuZmugy2c13INaAj0zQNDUMNYmW/wB "JavaScript (Node.js) – Try It Online"
#3: Post edited by user avatar Moshi‭ · 2021-09-06T21:29:03Z (over 3 years ago)
  • # [JavaScript (Node.js)], ~~47~~ 40 bytes
  • *-7 bytes thanks to Shaggy!*
  • ```
  • f=n=>n?f(n/3+(n%=3)/2|0)+"+-0+-"[n+2]:''
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcZBDoIwEADAs7xiQ2K6m02lwg2sPsR4INiakmZrgHBB315JPM2M/drPwxTei5b0dDl7K/YqN49SNYxytA1V9ccQl6wN6/IuXD9apbJPE0a3QAAL@mxMt@8C/zATbMVhSDKn6E4xvTAAg2pB7XgMRF3xzT8 "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], ~~47~~ ~~40~~ 50 bytes
  • *-7 bytes thanks to Shaggy!*
  • *Why is fixing the zero case so expensive*
  • ```
  • f=(n,s='0')=>n?f(n/3+(n%=3)/2|0,'')+"+-0+-"[n+2]:s
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcZBDoIwEADAs7xiQ2K6m6VS4QZWH2I8EKSmpNkaSryob68knmbm4TWkcfHPVUu8Tzk7i1Ilq4wie5aLQ6lbRtnblurmYyqliEvWhnV5FW5uXcouLhimFTxY0Edj@m0n@IeZ4F3sxigphukQ4gM9MKgO1IZDT9QX3/wD "JavaScript (Node.js) – Try It Online"
#2: Post edited by user avatar Moshi‭ · 2021-09-05T19:37:13Z (over 3 years ago)
  • # [JavaScript (Node.js)], 47 bytes
  • ```
  • f=(n,r=n%3)=>n?f(n/3+(r/2|0)|0)+"+-0+-"[r+2]:''
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcZBCsIwEEDRtT3FUJBMmMbGdtcaPYi4KDWRSJhIWtxozx4Dwof/ntN7WubkX6vieLc5O4PcJMP7XpozXxxy2xOmtvtqWaKalCZVXxN1t0GI7GLCYFfwYEAdtR6LTvAHkYRPtZsjLzHYQ4gP9EAgBhBlDr2UY7XlHw "JavaScript (Node.js) – Try It Online"
  • # [JavaScript (Node.js)], ~~47~~ 40 bytes
  • *-7 bytes thanks to Shaggy!*
  • ```
  • f=n=>n?f(n/3+(n%=3)/2|0)+"+-0+-"[n+2]:''
  • ```
  • [Try it online!][TIO-kt6t7cyt]
  • A basic recursive solution.
  • [JavaScript (Node.js)]: https://nodejs.org
  • [TIO-kt6t7cyt]: https://tio.run/##JcZBDoIwEADAs7xiQ2K6m02lwg2sPsR4INiakmZrgHBB315JPM2M/drPwxTei5b0dDl7K/YqN49SNYxytA1V9ccQl6wN6/IuXD9apbJPE0a3QAAL@mxMt@8C/zATbMVhSDKn6E4xvTAAg2pB7XgMRF3xzT8 "JavaScript (Node.js) – Try It Online"
#1: Initial revision by user avatar Moshi‭ · 2021-09-05T06:04:42Z (over 3 years ago)
# [JavaScript (Node.js)], 47 bytes

```
f=(n,r=n%3)=>n?f(n/3+(r/2|0)|0)+"+-0+-"[r+2]:''
```

[Try it online!][TIO-kt6t7cyt]

A basic recursive solution.

[JavaScript (Node.js)]: https://nodejs.org
[TIO-kt6t7cyt]: https://tio.run/##JcZBCsIwEEDRtT3FUJBMmMbGdtcaPYi4KDWRSJhIWtxozx4Dwof/ntN7WubkX6vieLc5O4PcJMP7XpozXxxy2xOmtvtqWaKalCZVXxN1t0GI7GLCYFfwYEAdtR6LTvAHkYRPtZsjLzHYQ4gP9EAgBhBlDr2UY7XlHw "JavaScript (Node.js) – Try It Online"