If i had to use reduce to combine combine an array into a string using the join method

If i had to use reduce to combine combine an array into a string using the join method
0

#1

Hi i was trying to get more familiar with reduce function. The question asks not to use the reduce method so i think it might be making the problem very simple. How can i apply reduce ? can i do both split and join inside reduce ?

current code not correct

function sentensify(str) {
  // Add your code below this line
  return str.reduce((a, b) => {
    b + ' ' + a;
  });
  
  // Add your code above this line
}
console.log(sentensify("May-the-force-be-with-you"));

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/combine-an-array-into-a-string-using-the-join-method


#2

Is this an FCC challenge? If it is, what is the url? If not an FCC challenge, then what exactly is this function supposed to do?


#3

reduce is an array method, so you have to use split and join before and after it, respectively. Things get awkward when you’re doing string manipulation with reduce, though.

 "THIS IS A STRING".split('')
    .reduce((acc, curr) => {
        acc.push(curr.toLowerCase());
        return acc;
    }, []).join(''); // "this is a string"

I wouldn’t suggest this. It actually hurts me to read.


#4

Here is a slightly different version with reduce where the accumulator starts as a blank string, which avoids having to use the join method at the end . Is this more readable?

const sentensify = str => [...str]
  .reduce((string, char) => string.concat(char.toLowerCase()), ''); 
  
sentensify("THIS IS A STRING"); // "this is a string"

#5

Sorry i had forgotten to put the link earlier. I have edited the original post and included the link in it.