A small JavaScript puzzle walk-through.

I came across this mini code challenge a while back. It’s the type of question that tech companies will sometimes use to test a programmer’s abilities before allowing them to interview.

This particular challenge is in JavaScript, but if you like math, you’ll follow along just fine!

Here’s the puzzle problem:

// Write one Javascript statement on the indicated line that will 
// make the printed number always be between 10 and 20.

let x = 2;
let y = 8;
const a = function(b) {
  return function(c) {
    return x + y + Math.abs(b) + c;
  }; 
};

// ***** Statement will go here *****

const fn = a(x);
x = 4;
console.log(fn(Math.random() * 10));

The original challenge.

Go ahead and try to solve it yourself.

Do you have an answer?

Great! Let’s step through it below and see if we arrived at the same solution.

#careers #javascript #interview

JavaScript Mini Code Challenge
1.55 GEEK