The Word Smith

The Word Smith
Macbook on white desk at dawn

"Every piece of string tells a story of highs and lows pointing their fingers at each other as if they are not one and the same."

Here's a question:

How does one reverse a word with code?

"This is beautiful erutcip"

An array. A pointer. A loop. A few variables.

In typescript you can, well, loop through each character in a string directly because it is, as we say, an iterable object. As such, it is not that there are pointers between each character but rather, Javascript strings are stored as an array of characters, so we can quite simple loop through each and every one, creating a new string as if by magic by copying the last element in the initial string array and placing it in the first position in the new string array.

for(character of oldString){
}

This array, being made one step at a time by simple addition:

for(character of oldString){
	newString = newString + character
}

And when used with our original term, we can finally see the full picture

oldString = erutcip
for(character of oldString){
	newString = newString + character
}

Sometimes we make things more complicated than they need to be. Sometimes we split() when we don't have to. Sometimes we don't understand the problem well enough. And sometimes all it takes is a small addition to the puzzle for everything to finally make sense.