JavaScript Problem Solving: Making Use of Call and Apply
Last week, I participated in an online event hosted by skilled. If you are unfamiliar with the site, basically they provide a platform for mock technical interviews. Pass or fail, I find it’s a worthwhile experience to check out their services if you are in the interviewing processes.
Anyone who has ever worked in software engineering can tell you that these types of assessments are the gatekeeper to getting a job offer. A resume only goes so far; these problems allow you the opportunity to prove your skills to a potential employer. So having confidence in what you know, being able to interact with your proctor, and having a good system for problem solving are vital to success.
I’m going to run down some of the advice I received that day, and then go through a problem that, at first, was a little tricky, but once I was able to apply (no pun intended) a few new strategies, it became a pretty fun and interesting process.
Before I jump into the lecturer’s advice, take a look at this article. This structure is pretty straightforward, but the one concept I’d like to add from the lecture is:
create a ‘feedback’ loop
This is not to say ‘create a loop’, like a recursive function, necessarily. It really means, set up test scenarios to give yourself feedback on the direction your code is headed. Which also does not necessarily mean to write, for example, an rspec file either.
It means, simply: 1.) console logging and 2.) calling the function at run-time.
Therefore, take a moment to review (links below) of some of JavaScripts most import native functions, then let’s move on: call and apply.
So now, without further ado, here is the prompt:
Let’s take a moment to understand what is being asked of us here. The first thing I heard is: we need to keep a count of how many times a function has been called; let’s take note of that, but continue to develop an idea of what is going on here first. Also, lucky for us, we have our ‘feedback’ loop presented for us (the sample calls)
So, moreover, we have two functions, func1 and func2, that will get passed into our after function by newFunc1 and newFunc2, along with x, or the amount of times newFunc1 or newFunc2 needs to be called before it can be executed. Also, notice: func1 console logs, and func2 takes a variable and also console logs. See the diff?
Also note, our function after needs to return a new function that will apply our original function (the function we pass in; in this case func1 or func2) after our count is greater than x.
Let’s build a pseudo-code skeleton to give ourselves an idea of what to do. First things first, the count will keep track of our calls. We’ll start it at 0, and increment it by 1 so long as the count < x times. Secondly, we’ll set the function we take in (ie func) equal to a variable called ‘theFunc’. Is it necessary you ask? Not entirely just to make it work, no. But, a copy of it is necessary to meet our given prompt of returning a “new function”. Ah, the devil’s in the details. Third, we’ll need an if/else statement to evaluate if our conditions have been met. Lastly, and here’s the whole point of my article’s title: we’ll use apply. In so doing, we’ll use apply on ‘theFunc’, which, if you’ve read into the MDN docs, takes first an object (ie ‘this’), then an array of arguments. Since that object is not bound to our original functions, we’ll set it to null, but still have our new ‘theFunc’ take in the argsArray, which we can say is arguments (it needs to take this). And the resulting code I come up with is:
This isn’t great because it isn’t very DRY. For instance, in our else statement, we can simply increment, like this: count +=1. Moreover, the return function can be simplified to taking in a variable, and having the original function use that parameter. It would look more like this:
Either one pretty much works. So, I’ll leave it up to you, my reader, to critique my work and give me feedback on how this code looks to you. Would you be ok with someone taking a technical interview and providing either of these answers, or I have far missed the mark? Seriously, I’d love to know: I’m trying to get better at this, and would appreciate all the help, advice, and JavaScript problems you can throw at me to practice.
I hope this blog served you well. Stay tuned for more JavaScript problem blogs coming at you this week, next, and beyond.