One of the first computing concepts I was introduced to when I started learning to program was the concept of binary numbers. I had known already that computers understood everything on a fundamendal level in terms of binary code, but I hadn't understood how standard numbers in base-10, for example, could be understood in base-2. Converting numbers to their representations in different bases was a bizarre concept to me until I was shown me how they are broken down: how 111 turned into 7, for example. Once I finally worked through it mentally, I sat down and wrote two scripts to translate a base-10 number into base-2, and vice versa, and then rewrote these programs in JavaScript. To start, here's the Ruby script that converts a base-2 number into a base-10 one:
Understanding that the binary number needed to be reversed before it could be translated was an important part of conceptualizing it: that a binary number like 001 needs to be seen as 100 in order to be passed as a number to the program, but that 100 must then be reversed in order to be translated. Here's the same script in JavaScript:
Conceptually, I've been trying to stick to the same logic in Ruby as well as JavaScript; I'm not sure if this really helps me branch out into the way JavaScript actually differs conceptually from Ruby, but it's certainly helped speed me up to solving algorithmic problems in JavaScript.
So far the programs I wrote to convert a base-2 to base-10 numbers were short. By contrast, my solution for converting a base-10 to base-2 turned out to be much longer:
I couldn't think of how to slot in the appropriate 1's into their places without counting up in powers of 2, but that solution did work well. At some point I want to return to it and think about it again to see if I could solve it in a better way. Here's the same solution in JavaScript:
That's all for today! Thanks for reading!