Learning to talk to a computer by reversing a string
When you start learning to code, one of the things you have to start figuring out is how to think like a computer. It follows that only once you know a bit about how a computer thinks, can you start to try and have a conversation with it.
Learning to speak with a computer as a Beginner is an intimidating process, but it’s always good to remember that the human brain itself is one of the most complicated machines. We may not realise it but we, like computers, are constantly processing complex data sets and using algorithms. In case you’re unfamiliar with the term, an Algorithm is essentially: “a set of rules that precisely defines a sequence of operations.” A part of beginning to learn to programme includes being introduced to Data Structures and hearing about different types of algorithms, from Bubble Sort, Binary Search to Sequential Searches.
Before being swamped by algorithms though, it’s possible to better understand how computers speak by thinking more about how they read your code. For example, when you’re writing a string each character of that string is read by the computer through its index. You can see this in the below picture from How to think like a computer scientist, which shows how the string “banana” has each letter indexed, starting from zero.

Getting to grips with the very basic fundamentals of the computer using an index structure was important for working through which method to take in reversing a string. You can see the method I took in reversing the string in the console using Javascript below. I started the process by firstly creating my variable and assigning it a string value: (var pet = “dogs”). From there, the next step was to know how the index correlated to each letter of the string (0,1,2,3) = (d,o,g,s). From printing out the “console.log” I knew that to a computer my variable “dogs” was four index numberslong, it started with at 0 with a “d”, and ended at 3 with “s”.
Having first worked out the “.length” and printed the “console.log”, I then started thinking through what approach i could apply from the Javascript I had learnt so far that would allow me to reverse the order of the string and print it out. From my previous codecademy exercises I remembered “For loops” which allow you to print the same code each time but under different values .
When I initially wrote out the loop I started at the variable index(“var i”) 0 letter “d”, but soon realised I needed to start in the location of the index where I wanted it to begin printing from the final letter- in this case the 3 “s”. I then set it to loop back to where the index is greater than or equal to run the string from “s” back to “d”. To keep counting backwards from 3, (and not be stuck in an infinite loop!) I added in the sum “i-=1”. Then printed the result “console.log”, instructing it to print the parameter of the variable “pet” and the index “i”, and voila! I found thar each letter was printed in reverse “s-g-o-d”.
So that’s it! From a Beginners perspective, there’s one method for learning a little about how to talk to computers by reversing a string.