Quiz 26: Scheduler
⚠️
Warning: Executing code will replace the current content on this page with the output produced by the executed code. To return to the quiz simply reload the page using
CTRL + R.💡
All questions in this quiz use the following definition of a Scheduler:
Scheduler
const Scheduler = () => {
let inProcess = false;
const tasks = [];
function process() {
if (inProcess) return;
if (tasks.length === 0) return;
inProcess = true;
const task = tasks.pop();
const prom = new Promise(ok => task(ok));
prom.then(_ => {
inProcess = false;
process();
});
}
function add(task) {
tasks.unshift(task);
process();
}
return {
add: add,
addOk: task => add(ok => {
task();
ok();
}) // convenience
}
};Question 1
Shows
'0,1'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
scheduler.add(ok => {
document.write(state);
ok();
});Question 2
Shows
'0,2'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
document.write(state);Question 3
Shows
'0'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
document.write(state);Question 4
Shows
'0'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
scheduler.add(ok => {
document.write(state);
ok();
});Question 5
Shows
'0,1'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
document.write(state);Question 6
Shows
'0' eventually and no other value?JavaScript
let state = 3;
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state -= 2; // Changes state to 1
ok();
}, 100);
});
scheduler.add(ok => {
state -= 1; // Changes state to 0
ok();
});
scheduler.add(ok => {
document.write(state);
ok();
});Question 7
Might show
'0' but might as well show '3' or '1'?JavaScript
let state = 3;
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state = 2;
ok();
}, 100);
});
scheduler.add(ok => {
state = state - 1;
ok();
});
scheduler.add(ok => {
document.write(state);
ok();
});Question 8
Shows
'0,1,2'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
document.write(state);Question 9
Logs
'x' and '0'?JavaScript
let state = 3;
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state = 2;
ok();
}, 100);
});
scheduler.add(ok => {
state = state - 1;
ok();
});
scheduler.add(ok => {
console.log(state);
ok();
});
console.log('x');Question 10
Logs
'0,1,2'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
scheduler.add(ok => {
document.write(state);
ok();
});Question 11
Logs
'0,1,2'?JavaScript
let state = [0];
const scheduler = Scheduler();
scheduler.add(ok => {
setTimeout(() => {
state.push(1);
ok();
}, 100);
});
scheduler.add(ok => {
state.push(2);
ok();
});
document.write(state);