Interview task with mistakes and .toString

Dmitry Yakimov
BeadList
Published in
3 min readJul 25, 2018

A few days ago my dearest friend applied to a job opportunity as a JS developer. He had to finish a series of programming exercises to pass to the next round.

And because I’m couching him in web development, we decided to go through tasks together. It’s not like I’m doing the work for him, but I just wanted to help with the tasks which he had difficulties with, after he completes them. This way we can see his pain points and work further on them.

He was doing great, and completed most of them. But frankly, some of the tasks were made with mistakes or the creators of the exercises were crazy enough to ask those bizarre questions.

Anyways one of those exercises caught my attention. It was presented like this.

// Implement the increment class
const increment = new Increment();
alert(increment); // 1
alert(increment); // 2

alert(increment + increment); // 7

I must say that the code originally used var syntax and overall the exercises had the early 2000s coding vibe, with a few questions related to jQuery.

So what we thought is that the author had missed the brackets after increment words in all these alert calls. Then, if fixed the task becomes like this:

// Implement the increment class
const increment = new Increment();
alert(increment()); // 1
alert(increment()); // 2

alert(increment() + increment()); //7

And this is pretty trivial task. And my friend found the solution quickly.

Just for fun, I decided to find a solution to the original task by making the use of toString method. Which is abnormal coding practice for such a task. But it works for original code:

class Increment {
constructor() {
this.value = 0;
}

toString() {
this.value++;
return this.value;
}
}

const increment = new Increment();
alert(increment); // 1
alert(increment); // 2

alert(increment + increment); // 7

What is interesting here, is that increment + increment returns the correct sum. So +increment statement returns a correct number. Which means first it calls toString method and then it converts its result to number. That’s a nice hacky trick, and it can substitute operator overloading in some cases. Although I wouldn’t use it for normal production code.

Another task was presented like this:

// Implement sum function
alert(sum(1, 2)); // 3
alert(sum(1)(2)); // 3

Pretty easy, eh? I would just write a simple function for 2 arguments max which would do the job. And I would happily move on to the next task. But my friend, all excited by the toString method, decided to create a function which would support unlimited arguments. He doesn’t like it easy.

He came up with something like this:

const sum = (...values) => {
const result = values.reduce((acc, v) => acc + v);
const next = (...newValues) => sum(result, ...newValues);
next.toString = () => result;
return next;
};

Looks neat. And now it works for all these scenarios:

alert(sum(1, 2)); // 3
alert(sum(1)(2)); // 3
alert(sum(1)(2)(3)); // 6
alert(sum(1, 2)(3)); // 6

alert(sum(2, 5)(3) + sum(5)(2)); // 17
alert(sum(2, 5)(3) — sum(5)(2)); // 3
alert(sum(sum(2, 5)(3))(sum(5)(2))); // 1

That actually looks pretty nice. It looks good for human eyes, and yet works. It brings interesting recursive function curring without need for .value() additions and such.

The caveat of it is if we replace alert with console.log it outputs strangely in chrome console:

ƒ 3

And I bet there are some other edge cases, where it would behave nasty.

Anyways, it was quite fun for me to see these tasks. Simple mistakes in interviewers code brought some joyful ideas and a pleasant time with coffee and code.

Good luck to everyone who is going through interviews!

--

--

Dmitry Yakimov
BeadList

Web-consultant, Tech-enthusiast. Working on BeadList App: beadlist.com