5 common JS quiz questions

Tinus Wagner
Mar 22, 2018 · 4 min read

I don’t know about the rest of you, but the interview tests generally scare the crap out of me. I’ve built quite a few things over the last two years that I’m truly proud of, but whenever the interview process heads down this road I always feel my nerves bubbling up.

So let’s dive into some common questions and solve em:

1) Make this statement work

Now my first instinct is simply to grab my array and concatenate by writing a little function. But that doesn’t really answer the question and shows how easily nerves can lead you in the wrong direction.


What you really want to do is dig into the Array constructor, in other words, you want to add a duplicator method to the object used to construct ALL ARRAYS.

To do this you need to dive into Array.prototype which

...Array instances inherit from .... As with all constructors, you can change the constructor's prototype object to make changes to all Array instances. For example, you can add new methods and properties to extend all Array objects.

We extend the core Array object with a method called duplicator

Don’t try to get fancy here as the ES6 arrow function does not have its own this, arguments, super, or new.target. (…), and they cannot be used as constructors.

2) Fizz-Buzz

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

This tests your knowledge of operators (or one operator) ever so slightly.

There’s a million ways to solve this one. But this solution does have a problems with repetition aaaaaaaaaand if we’d change the order of execution and place the 3 & 5 comparison last in the loop, it will never be executed. So let’s try again.

  • We can DRY up this loop by saving the outcome of our comparison in a var and then simply printing/logging our output at the end of our loop.
  • we can pull out our else statements since this will achieve the same purpose regardless
  • By saving the fizz/buzz output to a variable we can easily concatenate the string if both conditions are met which allows us to do away with an modulo comparison.

While looking at the hundreds of ways to solve FizzBuzz I stumbled on this post. Which broke down the shortest version of FizzBuzz I’ve ever seen, and in a way that doesn’t deviate to much from our second solution once you break it down.

Ternary Operators! That’s the secret here, each loop compares i to our conditions and either appends an empty string, or fizz or buzz. If no condition is matched, it simply returns i. And since all this already happens in a console.log, the content is printed.

3) How would you make this work?

This is quite an interesting one that I hadn’t come across before. In the first example add is obviously a function that returns the sum of both arguments. But we’ve the second examples makes use of a secondary set of brackets which are a bit WTF.

This is an example of a function, that internally calls itself. And when you really think it through it makes sense if the first executions value is saved as an variable internally, or intermediate variable. So we could visualise it like this:

I credit this stackoverflow answer for helping me get it. What we’re really dealing with here is the concept of closures which is

…the combination of a function and the lexical environment within which that function was declared.

Now that we understand that a closure is a combination of the function and the environment it is executed in we can do away with our intermediate variable since we understand that the secondary internal call of the function has access to a difference environment than the first, whereby the first argument becomes available for use within the second function call.

I’d recommend reading the practical use of closures section on MDN to get a feel for where they may be useful.

So now we can solve both use cases with one function by merging our approaches, by internally checking how many arguments are supplied, and conditionally executing a sum on the secondary set of brackets.

When a function is invoked, JavaScript does not require the number of arguments to match the number of arguments in the function definition. If the number of arguments passed exceeds the number of arguments in the function definition, the excess arguments will simply be ignored.

4) Implement your own Array flatten method

But we can simplify this even further using reduce to run a function against each value in the index.

5) Will the following statements return True or False?

This is quite a interesting little question since it deals with the order of execution.

As expected the first statement evaluates to true, however the second returns false because javascript. Comparison is done from left to right so in the end what’s really compared here is true > 1which evaluates to false

Thanks for reading!

Hey Tinus, that’s not a great solution to XYZ

Sweet, feel free to let me know!

Hey Tinus, you can ES6/7/8/22/101 that using XYZ

Sweet, feel free to let me know!

Tinus Wagner

Written by

Developer, Producer, Drummer, Synth lover.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade