Forgot to comment my code. Why does this work?

Forgot to comment my code. Why does this work?
0

#1

Here are the instructions:

We’ll be rebuilding the native .join array method. The join function should work as follows:

// If the input is empty, return an empty string
join([]) // ''

// If there is no separator, separate by commas
join([ 'a', 'b', 'c' ]) // 'a,b,c'

// If there is a separator, separate by that string
join([ 'a', 'b', 'c' ], '-') // 'a-b-c'

// If there is only one item, return just that item
join([ 'a' ], '---') // 'a'

I’m just a bit confused on this line:

if (separator && array[i] !== array[array.length-1])

Here is my code:

function join (array, separator) {
    let str = '';
    for (let i = 0; i < array.length; i++) {
      str += array[i];
      if (separator && array[i] !== array[array.length-1]) {
        str += separator;
      } else if (array[i] !== array[array.length-1]) {
        str += ',';
      }
    }
    return str;
  }

#2

Roughly in English, that means, “if there is a separator, and the ith element in the array is not the last element, do the following…”


#3

Thanks man. Appreciate it.


#4

Some other options for this challenge would be:

function join(array, separator = ',') {
  let str = '';
  let len = array.length;
  for (let i = 0; i < array.length; i++) {
	str += array[i] + separator;
	len++;
  }
  return str.slice(0, len - 1);
}

OR using a regular expression

function join(array, separator = ',') {
  const reg = new RegExp(',', 'g');
  return ('' + array).replace(reg, separator);
}

#5

You are insane at this. Truly. I can only hope at some point I get that good.


#6

Also, if you find time, could you comment that regular expression using RegExp? I’m trying to learn Regex as it seems to be the best way to solve a lot of things that I tend to complicate.


#7
new RegExp(',', 'g');

is the equilavent of:

const reg = /,/g;

I should have used the second version (know as literal notation) instead of the first version (known as a constructor), because a constructor was not really needed.

The more common version is the literal (the 2nd). Basically, it means find all instances of the comma character. The g means all instances. If left off, it would only find the first instance of a comma.

Rewriting the regular expression version of the function using literal notation would look like:

function join(array, separator = ',') {
  const reg = /,/g;
  return ('' + array).replace(reg, separator);
}

But since the regular expression is short, I could simply put it directly into the replace function.

function join(array, separator = ',') {
  return ('' + array).replace(/,/g , separator);
}

and if using arrow function syntax and const declaration, this function becomes a one-liner.

const join = (array, separator = ',') => ('' + array).replace(/,/g , separator);

Something I did not explain before was why I wrote '' + array. In Javascript, I can coerce another datatype to a string by concatenating it with a string. I used a blank string '' and concatenated it with array. When you concatenate a string to an array it turns it into a string of the elements separated by commas (no spaces). That is why I was able my regular expression looked for commas and then I used the replace method to replace the commas with whatever separator was passed into the function. If no separator was passed into the function, I gave the separator a default value of comma, which is denoted in the function declaration shown below:
join(array, separator = ',') {