25
loading...
This website collects cookies to deliver better user experience
const incompleteTasks = tasks.filter((task) => !task.completed)
const incompleteTasks = []
for (const task of tasks) {
if (!task.completed) {
incompleteTasks.push(task)
}
}
for (let i = 0; i < tasks.length; i++) {
if (task.completed) {
tasks.splice(i, 1) // Remove task number i
i-- // If we deleted task 4, task 5 will slide up into its spot, so we need to check task 4 again next
}
}
tasks.reverse()
or to add new tasks to the beginning of the array instead of the end.-1
, 1
, or 0
, depending on whether task A should be sorted before, after, or equally with B.tasks.sort((a, b) => {
if (a.dateCreated < b.dateCreated) return -1
if (a.dateCreated > b.dateCreated) return 1
return 0
})
tasks.sort((a, b) => {
return a.dateCreated < b.dateCreated ? -1 : 1
})
tasks.sort((a, b) => {
return a.dateCreated - b.dateCreated
})
a - b
directly, instead of a.dateCreated - b.dateCreated
, or to return true or false instead of 1 or -1. Nudge them toward the documentation if they're making these sorts of mistakes. Sometimes candidates try too hard not to look anything up during the interview even if they would be quick to do so on the job, so extra encouragement can help.Array().concat(tasks).sort
, tasks.slice().sort
, [...tasks].sort
, or by chaining sort after a map or filter operation. If they're having trouble with this one, explain the problem, but give them time to find their own solution.const completeTasks = tasks.filter((task) => task.complete)
const incompleteTasks = tasks.filter((task) => !task.complete)