I need help for this exercise:
Array methods
Implement the uncompletedNotes function which, given an array of notes, returns only the uncompleted notes. A note is considered completed if all todo present have the done flag set to true.
This is what I've done:
function uncompletedNotes(notes) {
let result = []
notes.forEach(element => {
if (element.todos.done == false) {
result.push(element);
}
});
return result;
}
const notes = [{
id: 1,
description: 'Workout program',
todos: [{
id: 1,
name: 'Push ups - 10 x 3',
done: false
},
{
id: 2,
name: 'Abdominals - 20 x 3',
done: true
},
{
id: 3,
name: 'Tapis Roulant - 15min',
done: true
}
]
},
{
id: 2,
description: 'Front-end Roadmap',
todos: [{
id: 1,
name: 'Learn HTML',
done: true
},
{
id: 2,
name: 'Learn CSS',
done: true
},
{
id: 3,
name: 'Learn JavaScript',
done: true
},
{
id: 4,
name: 'Learn Angular',
done: true
}
]
}
]
const notesInProgress = uncompletedNotes(notes);
console.log('All notes: ', notes);
console.log('Notes In Progress: ', notesInProgress);
But it gives me an empty array result in Notes in Progress.
function uncompletedNotes(notes) {
return notes.filter(note => note.todos.filter(todo => !todo.done))
}
The problem here is that the todos property of each notes is itself an array, ie there is no done property on todos. It may be useful to look at the array methods every or some (likely some), which will return true if any item in an iterable returns true for a given condition.
function uncompletedNotes(notes) {
let result = []
notes.forEach(element => {
if(element.todos.some(todo => todo.done === false)) {
result.push(element);
}
});
return result;
}
Todos is also an array, so you'd have to iterate over that one as well