Left-Pad could be the next FizzBuzz so we coded it up in 13 Languages

Left-Pad has generated so much buzz that we jokingly said that it could become the next FizzBuzz, and then we realized that that might be the case.

Like many other engineers, our team at Educative was discussing the situation created by Azer Koçulu's decision to unpublish his NPM module 'Left-Pad'. If you have been living under the rock, look here.

David (who says that he's mostly known as Haney) suggested that we should be not taking dependencies for simple functions like Left-Pad.

One of us jokingly stated that this could become the next FizzBuzz question. We joked about a few possibilities and then realized that the programming world might be on the verge of discovering the new FizzBuzz question - named Left-Pad. We then decided to ensure that the developers of the planet Earth are ready to embrace their next FizzBuzz problem.

You might be heading for your coding interview tomorrow unaware that the rules of the game have been changed. Instead of FizzBuzz, you are going to encounter a far more precarious string concatenation puzzle - LeftPad. Don't worry, we decided to act fast and are giving you code for Left-Pad in 13 languages so that you are ready no matter what.

[This is just for fun but if you are preparing for interviews, look at our course Coderust 2.0: Faster Coding Interview Preparation using Visualizations].

Let's look at what our LeftPad problem entails. The requirements are simple. You are passed a string, the target length of the string and a character (or string of length 1 in languages that don't have a char type) to pad on the left until the resultant string has achieved, at least, the required length. When a pad character is not provided, consider the default character to be a period.

A few examples are:

leftPad("foo",  6) ---> "...foo"

leftPad("foo", 3) ---> "foo"

leftPad("foobar", 3) ---> "foobar"

leftPad("foo", 6, "?") ---> "???foo"

Next, we will look at the implementations of our beloved leftpad in different languages starting with Javascript. Let's start looking at them.