I'm currently practicing the .reduce
method in JavaScript and I have to turn an array of numbers into a string of all those numbers using the method mentioned before.
function stringConcat(arr) {
return arr.reduce((acc, el) => acc.toString(el));
}
console.log(stringConcat([1, 2, 3])); // expected result is "123"
Try this:
The idea is to concat acc
with el
in every iteration, here is an example for [1,2,3]:
iteration 1: acc = '', el = 1 return '1'
iteration 2: acc = '1', el = 2 return '12'
iteration 3: acc = '12', el = 3 return '123'
function stringConcat(arr) {
return arr.reduce((acc, el) => `${acc}${el}`);
}
console.log(stringConcat([1,2,3])); // the expected result is "123"
Learn more about reduce()
Cheers!