0

I want to simulate join function using reduce in javascript.And here is the code:

function join(arr, separator) {
    return arr.reduce((a, item) => (a ? a + separator + item : item), "");
}

But when i try join(['', '', ''], ','), the code produce ''. I don't understand why my code doesn't output ',,'.

Abito Prakash
  • 4,368
  • 2
  • 13
  • 26
  • 1
    Can you explain the purpose of `a ? … : …` in your code? – Bergi Jan 10 '23 at 05:03
  • if a is not empty , a+separator +item will be returned, otherwise item will be returned –  Jan 10 '23 at 05:04
  • 1
    Well yes that's what it does, but *why* did you do that? When did you expect `a` to be empty? Hint hint - the problem is that `a` can also be empty when you didn't expect it. – Bergi Jan 10 '23 at 05:17
  • An empty string is [falsey](https://stackoverflow.com/a/19839953/1563833). in JavaScript, so if the first string is `''`, then the expression `a` is falsey and `a ? ...` results in `item`. – Wyck Jan 10 '23 at 05:20

3 Answers3

1

Issue with your code is

a ? a + separator + item : item

You are validating value but you need to validate index.

Idea:

  • You need to add a separator between 2 values.
  • Either put it before or after.
  • For adding before, you will have to skip first iteration.
  • For adding after, you will have to skip last value

function join(arr, separator) {
  return arr.reduce((acc, item, index) =>
    `${acc}${!index ? '': separator}${item}`,
    ''
  )
}

console.log(join(['', '', '', ''], ','))
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • {!index ? '': separator} is only for skipping the first iteration ,not for skipping last value ,right? –  Jan 10 '23 at 06:34
  • Yes. For skipping last, you will have to do `${ index === arr.length -1 ? '' : separator}` – Rajesh Jan 10 '23 at 07:45
0

Javascript doesn't evaluate '' as a truthy value, therefore this expression a + separator + item is never evaluated and item is always returned instead.

Your code should work if you input values like this: join(['a', 'b', 'c'], ','). However, if you would like join(['', '', ''], ',') to return as ',,,' then you can change your code to be something like this:

function join(arr, separator) {
    return arr.reduce((a, item) => ((item !== null || item !== undefined) ? a + separator + item : item), "");
}
cdallava
  • 61
  • 4
-1

Try below given code -

function join(arr, separator) {
    let result = arr.reduce(function (s, a) {
        return s + (a ? a + separator : a);
    }, '');
    
    console.log(result);    
}

In your code, you were using the wrong args for reducer function.

The first arg is the current result of the reducer function (think of it as the current accumulation of result of the inner functions) being processed by reducer function and second arg is the current array element.

Utkarsh
  • 105
  • 4
  • "*The first arg is the current element of array being processed by reducer function and second arg is the index of the current array element.*" - uh, [no](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)? – Bergi Jan 10 '23 at 05:18
  • what is the use index as argument to function after reduce? –  Jan 10 '23 at 05:20
  • Updated answer, wrote wrong info. Thanks for pointing out – Utkarsh Jan 10 '23 at 08:42