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