The Word Smith
"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(characterPosition in oldString){
}
This array, being made one step at a time by simple addition:
for(characterPosition in oldString){
newString = newString + oldString[oldString.length - 1 - characterPosition]
}
And when used with our original term, we can finally see the full picture
oldString = "erutcip"
newString = ""
for(characterPosition in oldString){
newString = newString + oldString[oldString.length - 1 - characterPosition]
}
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.
newString = "picture"
Subscribe to my newsletter
Read articles from Swae directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by