Answer the question
In order to leave comments, you need to log in
Why is the arr argument equal to undefined?
As far as I understand, on the first call to reduce, the value of the arr parameter should be the second parameter of reduce. But for some reason it is equal to undefind.
function camelize(str) {
array = str.split('-');
console.log(array);
let filteredArray = array.reduce((arr, item) => {
console.log(arr);
if(item == ''){
return;
}
}, []);
}
camelize('-webkit-transition')
Answer the question
In order to leave comments, you need to log in
At the first call, everything is in order, the input is an empty array.
But it is necessary to return something for the next iteration.
Now at the second call on the inputarr === undefined
'aBc-dEf-GH' => ['aBc', 'dEf', 'GH']
map()
пробежит по каждому элементу, возвращая новый. 'dEf' => 'Def'
'aBc' => 'abc'
function camelize(str) {
return str.split('-')
.map((word, i) => i ? word.substr(0, 1).toUpperCase() + word.substr(1).toLowerCase() : word.toLowerCase())
.join('');
}
You need to return an array, otherwise it will automatically return undefined.
I recommend watching this vid .
let filteredArray = array.reduce((arr, item) => {
console.log(arr);
if(item == ''){
return;
}
return arr // надо добавить эту строку
}, []);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question