Iteration Flashcards

1
Q

What does this log to console:
class Todo {
static DONE_MARKER = “X”;
static UNDONE_MARKER = “ “;

constructor(title) {
this.title = title;
this.done = false;
}

toString() {
let marker = this.isDone() ? Todo.DONE_MARKER : Todo.UNDONE_MARKER;
return [${marker}] ${this.title};
}

isDone() {
return this.done;
}

}

let todo1 = new Todo(“Buy milk”);

console.log(todo1.toString());

console.log(todo1);

console.log(String(todo1));

A

It seems String() just runs toString on whatever is passed to it:

[ ] Buy milk
Todo { title: ‘Buy milk’, done: false }
[ ] Buy milk

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s a better way of doing this:
class TodoList {
// Omitted code

isDone() {
let done = this.todos.filter(todo => !todo.isDone());
return done.length === 0;
}
}

A

class TodoList {
// Omitted code

isDone() {
return this.todos.every(todo => todo.isDone());
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly