30 Days of Code – Day 6: Let’s Review

30 Days of Code – Day 6: Let’s Review

Task: Basically take everything learned up to this point in #30daysofcode to complete the assignment. The goal requires strings to go through functions and end up output as a string where the even letters of the string (starting on 0) are printed first, then a space character, and then the odd letters of the string. So, “Hacker” (012345) comes out as “Hce akr” where the left letters are even and right are odd.

Process: Take the given strings (input) and turn them into an array. The demo fed in the strings “2 Hacker Rank” (each word separated by a \n newline). Then, push each character into an odd or even string depending on its number (starting from 0). This whole problem was much tougher than I thought it would be.

First, here is my code which will be explained in heavy detail below.

function processData(input) {
    //Ie: "Hacker" and  "Rank" becomes:
    //"Hce akr" even values - odd values [H, a, c, k, e, r] equals [0, 1, 2, 3, 4, 5]
    //"Rn ak" even values - odd values [R, a, n, k] equals [0, 1, 2, 3]
    var myArray = input.split("\n"); //Splitting string between each newline into myArray values.
    //input is: "2\nHacker\nRank". myArray = [[2], ["Hacker"], ["Rank"]], [[0], [1], [2]]
    var evenChar = "";
    var oddChar = "";

    for (var i = 1; i < myArray.length; i++){ //Starting from 1 (Hacker) to length of array (3 values) & incrementing by 1.
        for (var j = 0; j < myArray[i].length; j++) {//j is counting but array is already set to start at "Hacker" element.
            if (j%2 == 0) { //If j mod 2 equals 0, that means it's an even number.
                evenChar += myArray[i][j]; //Therefore, add value to evenChar string.
            } else {
                oddChar += myArray[i][j]; //If odd, add value to oddChar string.
            }
        }
        console.log(evenChar + " " + oddChar);
        evenChar = "";
        oddChar = "";
    }
}

Where to begin… Arrays are a weak point of mine and as such I required a lot of self-explaining of the code to grasp how this really worked. What I found best was to simply write out a heck of a lot of comments (seen above) as well as another explanation which actually listed out the results of these for loops.

Here is a breakdown line for line.

For reference, “input” is filled with “2\nHacker\nRank” for the default test case on HackerRank.

var myArray = input.split("\n"); 

myArray is initialized with input split at each \n.
myArray therefore looks like this [“2”, “Hacker”, “Rank”]. It has a length property of 3. “2” is 0 element, “Hacker” is 1, and “Rank” is 2, or [0, 1, 2].

    var evenChar = "";
    var oddChar = "";

Initializing evenChar and oddChar which we’ll be using to output our even and odd strings later.

for (var i = 1; i < myArray.length; i++){ 

Starting at i = 1, loop through until i is no longer less than myArray.Length, which is 3. This means we will loop through twice. Once when i = 1 and then when i = 2. See “Output” section for results of these loops in more detail.

Note: i is set to 1 because we do not want to access the first entry in the array (0 entry) which is “2” for the purposes of this problem. They only wanted output for Hacker Rank text.

for (var j = 0; j < myArray[i].length; j++) {

Now, when i is sitting at 1 (or 2 in the next loop), start j at 0 and continue the for loop until j is not less than myArray[i].length. For example, myArray[i].length will first end up as myArray[1].length and that is 6 because myArray[1] is “Hacker” and that string’s length is 6 characters.

if (j%2 == 0) { 

When we are counting through the characters of “Hacker” (and later “Rank”), we must check whether they are even or odd. This is accomplished with this if statement. If j%2 = 0 then it is even. If j%2 != 0 then it is odd.

evenChar += myArray[i][j]; 

If true, myArray[i][j] is added onto evenChar string. We’re specifically adding one letter at a time from the given array element. For example, in the first run, myArray[1][0], which gives us “H” in “Hacker” specifically. 1%1 == 0 checks out.

Note: Count starting with 0. I was confused initially because I wanted to tag H as an odd value, but it is 0.

else oddChar += myArray[i][j];{

If not true, then we’ve got an odd value on our hands and as such need to add that character to oddChar string. For example, in the second run of the nested for loop, myArray[1][1], which gives us “a” in “Hacker” specifically. 1%2 !==0.

console.log(evenChar + " " + oddChar);

Finally, the easiest part. Print out evenChar, space, oddChar. This should show us “Hck akr” for the original input of “Hacker” and then “Rn ak” for “Rank”.

        evenChar = "";
        oddChar = "";

evenChar and oddChar must be cleared because they are still within the larger for clause. After the inner one evaluates, i increments by 1 and we go again. If we did not clear the strings, they would keep the previous values and add the next strings onto them.

Output

Now here are the notes I took regarding what happens specifically with the running of the for loops as it helped me figure out what was going on to begin with.

1st run of for loop:
i = 1, j = 0.
We look at myArray[1].length and myArray[1] is “Hacker” which is 6 characters.
If we’re at an even value (j%2==0 determines if number is even or odd) then… evenChar gets myArray[i][j] put into it.

  • myArray[1][0] is even (j%2 evaluates to 0), so we collect “H” from “Hacker” and add to evenChar.
  • myArray[1][1] is odd (j%2 does not evaluate to 0), so we collect “a” from “Hacker” and add to oddChar.
  • myArray[1][2] is even, so we collect “c” from “Hacker” and add to evenChar.
  • myArray[1][3] is odd, so we collect “k” from “Hacker” and add to oddChar.
  • myArray[1][4] is even, so we collect “e” from “Hacker” and add to evenChar.
  • myArray[1][5] is odd, so we collect “r” from “Hacker” and add to oddChar.

6 is not less than myArray[6].length so this for loop concludes. We receive a printout of evenChar oddChar. evenChar and oddChar is cleared because they’ll need to hold values for “Rank” next.

2nd run of for loop (i = 2; 2 < myArray.Length(which is 3); 2++)
j (0) < myArray[2].length where length is now 4, representing “Rank”.

  • myArray[2][0] is even, so we collect “R” from “Rank” and add to evenChar.
  • myArray[2][1] is odd, so we collect “a” from “Rank” and add to oddChar.
  • myArray[2][2] is even, so we collect “n” from “Rank” and add to evenChar.
  • myArray[2][3] is odd, so we collect “k” from “Rank” and add to oddChar.

Original for statement does not run again either, as i (now incremented to 3) is not < myArray.length (which is 3).

Advertisements

One thought on “30 Days of Code – Day 6: Let’s Review

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s