What do you think the following code does?
function fn(msg, time) {
setTimeout(function() {
console.log(msg);
}, time);
}
fn('it works', 1000);
What do you think the following code does?
function fn(num) {
return (num > 3) ? 'TOO BIG' : num;
}
console.log([ 1, 2, 3, 4, 5 ].map(fn));
What do you think the following code does?
function fn(num, range) {
for (var i = 1; i <= range; i += 1) {
console.log((num % i) ? 'not divisible by' : 'divisible by', i);
}
}
fn(200, 10);
I put together these questions to try to assess whether a person with limited or no programming experience could understand what's going on. This code is actually fairly testable, as well, by simply overriding the definition of
console.log
for the sake of the test.Just as important as making sure code is testable is making sure that it meets its intended purpose, and part of what I wanted to assess was whether a person could make a good guess about the meaning of
console.log
:) I also didn't want to get involved in the concept of functions that return functions, for the sake of this assessment.