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
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.
Sorry i had forgotten to put the link earlier. I have edited the original post and included the link in it.