DEV Community

Seth
Seth

Posted on • Edited on

Zigzag Direction

We've got 2 parameters in this function: a string, and then a certain number of rows.

For example, 'PAYPALISHIRING' is the final result we want to get. But in order to get there, we need to separate it by the number of rows and join the rows together.

P A H N
A P L S I I G
Y I R

var convert = function(s, numRows) {

    if (s.length < 2 || s.length < numRows){
        return s
    }
Enter fullscreen mode Exit fullscreen mode

Looking at this first part, we are just saying if the length is only 1 letter or if the amount of letters is less than the amount of rows, just return the letter. This certifies our base condition.

var convert = function(s, numRows) {

    if (s.length < 2 || s.length < numRows){
        return s
    }

    const rows = new Array(numRows).fill('')
    let count = 0
    let reverse = false;

Enter fullscreen mode Exit fullscreen mode

Now we will set ourselves up for success. We are going to create a new array for the amount rows there are and will fill it with an empty string. If we consoled logged here with numRows = 3, it'd look like this:

var convert = function(s, numRows) {

    if (s.length < 2 || s.length < numRows){
        return s
    }

    const rows = new Array(numRows).fill('')
Enter fullscreen mode Exit fullscreen mode

If we console log rows, we get this below:

['']['']['']

Enter fullscreen mode Exit fullscreen mode

Next, we need a counter variable of 0, since we will later traverse the string and let each letter sit in an Array. Then, if the count is 0, or equal to the number of rows, we change direction.

Since we go left to right, we will hit the numRows before we hit 0 (the first letter in the first array is at 1, not 0, so we will want to say if reverse is true, then decrement down). If it's false, then keep going forward.

var convert = function(s, numRows) {

    if (s.length < 2 || s.length < numRows){
        return s
    }

    const rows = new Array(numRows).fill('')
    let count = 0
    let reverse = false;

    for (let i = 0; i < s.length; i++) {
    rows[count] += s[i]
    reverse ? count-- : count++
    if (count === 0 || count === numRows - 1) {
       reverse = !reverse
    }
}
Enter fullscreen mode Exit fullscreen mode

If we console log here, we can see the following happen: At the first array, we get the current letter and so on until we hit the 3rd letter. Then reverse changes course so the A, string 2, gets the next letter and we go down until we hit 1 and then

[ 'P', '', '' ]
   -
   1
[ 'P', 'A', '' ]
        -
        2
[ 'P', 'A', 'Y' ]
             -
             3
[ 'P', 'AP', 'Y' ]
        -
        2
[ 'PA', 'AP', 'Y' ]
   -
   1
[ 'PA', 'APL', 'Y' ]
          -
          2
[ 'PA', 'APL', 'YI' ]
                - 
                3
[ 'PA', 'APLS', 'YI' ]
           -
           2 
[ 'PAH', 'APLS', 'YI' ]
    -
    1

Enter fullscreen mode Exit fullscreen mode

And on and on we go, but we can see that when we hit one end we zag. If we hit the other way then we zig. The only thing left is to join all the rows together and return it.

var convert = function(s, numRows) {

    if (s.length < 2 || s.length < numRows){
        return s
    }

    const rows = new Array(numRows).fill('')
    let count = 0
    let reverse = false;

    for (let i = 0; i < s.length; i++) {
        let current = s[i]
        rows[count] += current
        console.log(rows)
        reverse ? count-- : count++
        if (count === 0 || count === numRows - 1) {
            reverse = !reverse
        }
    }
   let joined = rows.join('')
   console.log(joined)
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)