Wednesday, May 25, 2016

a/A Practice Problems: Reverse a String

When I had first learned about App Academy I quickly tried to learn everything I could to solve those damned practice problems. Short story shorter, I could not wrap my mind around the first one. I came back to it the other night, however, and with all my new fandangled Javascript skills I was able to take on more than a couple. SHABOOYAH. What really helps me when I am learning something new is seeing the process someone went through to solve the challenge at hand, in this case reversing a string without using the reverse method. Because I found so few resources like that initially, I thought to do it myself and hopefully be that source for someone else.

Note, App Academy is a Ruby/Rails school, but if I read correctly they'll allow you to answer challenges in whichever language you choose -- obviously I'll be doing all these in JS.

Your mission: "Write a method that will take a string as input, and return a new string with the same letters in reverse order"

Disclaimer: This is my methodology, there are many other ways to do this; most likely there better ways to do this. But I'm a noob, so shut it.

Step 1: Write out what you need to do. It's like a game plan or blueprint to getting the answer. It helps me to really think about what the question is asking so that I can answer appropriately. This one is pretty straightforward.

     >> have to be able to accept input (function via arguments/parameters) 
     >> pinpoint last letter of string (1 less than length)
     >> run through & return letters from last character (loop with descending counter)
     >> return reversed string

Step 2: Define the function. Only rules that apply are the same for that of variables. (no numbers, symbols to start off; contains only letters, numbers, $ and _). Set up function to accept one parameter, "word" in this example.
  
function reverseString (word) {}

 Step 3: Find last character of string. I set the value equal to a new variable, lastChar so that it's easier to access. Remember indexes start with 0 (hence the minus 1).

                                                       function reverseString(word) {                                                  
                                                         var lastChar = word.length - 1;
                                                        }

Step 4: For loop with descending counter. Instead of setting i = 0, I set it to lastChar so that it would start with the last letter of the string; i >= 0 tells the loop to stop once it reaches the index 0, first letter of the string; i-- tells the loop to decrement by one each time.

                                              function reverseString(word) {
                                                 var lastChar = word.length - 1;
                                                 for (var i = lastChar; i >= 0; i--) {
  
                                                 }
                                              }

Step 5: Concatenate. If we were to give ourselves a test case, say reverseString("abc");, and if we were to console.log(word[i]) we would see that each letter would come back in reversed order. Hooray! But, (boo!) it wouldn't come back in one string, "cba". To fix this, I thought do declare another variable (empty this time) and then add each letter to it, aka concatenate it into one. The code block inside the for-loop is telling the function to add each letter to the variable newString. I'm not sure why you would use bracket notation for a string when they're reserved mostly for arrays, but I know it works and that they operate similarly and that's the best explanation I can give.

                                              function reverseString(word) {
                                                 var lastChar = word.length - 1;
                                                 var newString = "";
                                                  for (var i = lastChar; i >= 0; i--) {
                                                     newString += word[i];
                                                           
 // same as newString = newString + word[i];
                                                  }
                                              }

Step 6: Return newString. Now that we have all the working parts, the only thing left to do is to return the reversed string. Remember to put this after the function.

                                              function reverseString(word) {
                                                var lastChar = word.length - 1;
                                                var newString = "";
                                                 for (var i = lastChar; i >= 0; i--) {
                                                     newString += word[i]; 
                                                 }
                                                 return newString;
                                              }

If you were to type this into your console or whatever environment, you should get "cba". There you have it, guys! Comment with any questions or if you have any techniques that would solve this quicker, I'd love to read it. Good luck!

No comments :

Post a Comment