Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created May 11, 2012 00:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save rmurphey/2656813 to your computer and use it in GitHub Desktop.
Save rmurphey/2656813 to your computer and use it in GitHub Desktop.
assessment

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);
@maxw3st
Copy link

maxw3st commented May 11, 2012

<deleted because the point is not to post the answers here>

@bernhard-hofmann
Copy link

As much fun as it is to test our ability to parse and compile code, I can't help but feel we ought to focus more on testing that code does what we expect. Out of interest I considered looking at the above from a unit testing perspective and asked myself how I would test each code block. The first thing that becomes clear is that the code is not testable because it uses console.log and should be changed to output it's values. It becomes a more interesting exercise to change the above code blocks to testable units and write the tests that assert our expectations and see where we failed.

@rmurphey
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment