let arr = Array.from(arguments);
arrMap = arr.map(item => Object.values(item));
arrReduce = arrMap.reduce( function ( acc, subArr ) {
subArr.reduce( function ( total, elem ) {
total = total + elem
console.log(total)
}, 0)
}, [])
{
'algebra': 6,
'history': 8,
'physics': 9,
'geography': 2,
'chemistry': 9
}
I need to buy a bicycle for my first son.' // the sum of the marks is the highest in the first diary.
'I need to buy a bicycle for my second son.' // the sum of the marks is the highest in the second diary.
'I need to buy a bicycle for my third son.' // the sum of the marks is the highest in the third diary.,
мне кажется надо использовать один reduce в другом
function whoseBicycle(...diaries) {
const [ [ son ] ] = [ 'first', 'second', 'third' ]
.map((n, i) => [
n,
Object.values(diaries[i]).reduce((acc, m) => acc + m, 0),
ageTable[`${n}SonAge`],
])
.sort((a, b) => (b[1] - a[1]) || (a[2] - b[2]));
return `I need to buy a bicycle for my ${son} son.`;
}
arrReduce = arrMap.reduce( function ( acc, subArr ) { subArr.reduce( function ( total, elem ) { total = total + elem console.log(total) }, 0) }, [])
когда выводишь в консоль total - отображается NaN, откуда он берется?
откуда в коде из спойлера берется ageTable?
поиск минимума сортировкой, это явно не то что стоит подсказывать новичкам
function whoseBicycle(...diaries) {
const son = [ 'first', 'second', 'third' ]
.map((n, i) => [
Object.values(diaries[i]).reduce((acc, m) => acc + m, 0),
ageTable[`${n}SonAge`],
n,
])
.reduce((son, n) => (son[0] > n[0] || (son[0] === n[0] && son[1] < n[1])) ? son : n, [ -1, 1 / 0 ])
.pop();
return `I need to buy a bicycle for my ${son} son.`;
}
ты складываешь не числа, а строки.
function whoseBicycle(dairy1, dairy2, dairy3) {
const son = ['first', 'second', 'third']
let diaries = Array.from(arguments);
const diariesMap = diaries.map(function(diary){
const values = Object.values(diary)
return values.reduce((acc, cur) => acc + cur, 0);
})
let max = Math.max(...diariesMap);
return `I need to buy a bicycle for my ${son[diariesMap.indexOf(max)]} son.`;
}