Bin2Dec
Status: Complete!
Tier: 1-Beginner
The first entry in the app-ideas that I’ve solved is Bin2Dec. It’s a straightforward binary to decimal converter meant to help developers learn base 2 math. While I’ve known about binary and what it is for some time, I haven’t had to actually really deal with it, so this will be a learning experience for sure! Let’s go over the criteria:
Constraints:
- Arrays may not be used to contain the binary digits entered by the user
- Determining the decimal equivalent of a particular binary digit in the sequence must be calculated using a single mathematical function, for example the natural logarithm.
User Stories:
- User can enter up to 8 binary digits in one input field
- User must be notified if anything other than a 0 or 1 was entered
- User views the results in a single output field containing the decimal (base 10) equivalent of the binary number that was entered
- (Bonus) User can enter a variable number of binary digits
The Solve
There are a few ways to solve for binary to decimal conversion and the way I’ve done it is via the “doubling” method. (This wikihow page has a great example of how to do it) I’ll go over my actual solve for this in a moment.
The Stack
For this project and for as many others as I can with this setup, I’ve used a Rails server hosted on Heroku. The server is routed to by my NGINX proxy that also manages routes to my blog. Right now, I’ve got a custom route for this project and then a catch-all for its assets. This isn’t really optimal, so I may try to find a different way to set things up, I’ve just gotta sort out at what point I want my blog to stop managing things and the projects box to take over. Right now /projects is on the blog which makes creating a general /projects catch all difficult.
Anyway, for the actual processing of the conversion, I’ve got a really simple Coffeescript function set up to manage things. This was my first time with Coffeescript and it’s interesting!
@convert = (input) ->
# "Error" on anything that's not a 0 or a 1.
if /[^01]/.test(input)
return "Please try again with only 0's, and 1's."
out = 0
# Do the conversion with, "a single mathematical function"
for i in input
out = out * 2 + parseInt(i, 10)
return out
I haven’t styled the page much, that’ll come later.
I’ve got the first of many projects finished though and this is the first step! :)
Thanks for reading!
Js