Skip to main content

You Can't Javascript Under Pressure

· 5 min read
Nishant Mendiratta

A fun little quiz. The quizzes get more and more difficult, and they present a nice challenge if you fear your skills are getting rusty. Give it an honest try first, then come back here if you get seriously, honestly stuck. Try to do it yourself!

Problem 1: doubleInteger

function doubleInteger(i) {
// i will be an integer. Double it and return it.

return i * 2;
}

Explanation

Nothing tricky going on here. The prompt implies this should be done in two steps, but a one-liner works. We know i will always be an integer, so we don’t need any sanitization checks, just a straight up return.

Problem 2: isNumberEven

function isNumberEven(i) {

// i will be an integer. Return true if it's even,
// and false if it isn't.

return i % 2 === 0;
}

Explanation

The solution relies on an understanding of the modulus function. Basically, think of modulus as “remainder” function for division problems. For instance, 4 % 2 will give a modulus of 1 because 4 goes into 2 twice, evenly, with zero remainder. On the other hand, 5 % 2 will give a modulus of 1 because 5 goes into 2 twice, with a remainder of 1. This turns out to be a handy way to differentiate even (4) and odd (5) numbers.

Problem 3: getFileExtension

function getFileExtension(i) {

// i will be a string, but it may not have a file extension.
// return the file extension (with no period) if it has one,
// otherwise false

var e = i.search(/\..+/)
if (e !== -1) {
return i.substr(e + 1);
}
else {
return false;
}
}

// Although above passes the test,
// this is a much more reliable solution.
function getFileExtension(i) {
var result = i.split('.');
return result.length > 1 ? result.pop() : false;
}

Explanation

Now we’re getting a bit tougher! Strings have a handy function that splits them into an array: split(). For instance, "foo.bar.baz".split(".") will output ["foo", "bar", "baz"]. Notice that the periods have been removed, just as the prompt asked for!

In this case we need to first check if there was no file extension. This means that there were no periods to split on, resulting in a one-member array ("foo".split(".") becomes ["foo"]). In this case we return false, as the prompt asks for.

Now, in the else case, we know that the array must be larger than 1, meaning that it has a file extension. Since the file extension is always at the end, we know that it must be the last member in the array.

We can find the index of the last member by taking into account array length (result.length) and the fact that the array is zero-indexed, meaning that we need to subtract 1 from this length. Now we know the extension exists and is at the end of the array, so all we need to do is return it:

Problem 4: longestString

function longestString(i) {

// i will be an array.
// return the longest string in the array

var longest = ''
if (Array.isArray(i)) {
for (var n = 0, len = i.length; n < len; ++n) {
if ((typeof i[n] === 'string') &&
(i[n].length > longest.length)) {
longest = i[n];
}
}
}
return longest;
}

// Alternate solution with some
// functional programming thrown in.
function longestString(i) {
var longest = '';
if (Array.isArray(i)) {
var a = i.filter(function(t) {
return typeof t === 'string';
}).forEach(function(t) {
if (t.length > longest.length) {
longest = t;
}
});

return longest;
}
}

Explanation

Just keep track of the longest string in a variable, then loop through all the members of the array. If the current member is longer, set it as the new longest string. At the end, return whatever that variable contains.

Problem 5: arraySum

function arraySum(i) {

// i will be an array, containing integers, strings
// and/or arrays like itself. Sum all the integers you
// find, anywhere in the nest of arrays.

var sum = 0;
if (Array.isArray(i)) {
for (var n = 0, len = i.length; n < len; ++n) {
if (Array.isArray(i[n])) {
sum += arraySum(i[n]);
}
else if (typeof i[n] === 'number') {
sum += i[n];
}
}
}
return sum;
}

Explanation

The best way to handle this one is through recursion, which is tricky but powerful.

First, let’s start with arraySum(1). The output is of course 1, but note that we didn’t actually input an array. The code took the first codepath in the if statement (because typeof 1 === "number" is true)

Let’s see what happens when we input a basic array:

arraySum([1]); // 1

Again the answer is 1, but this time the codepath was different. This time Array.isArray([1]) was true, and the function was called recursively for each of the members in the array. That is to say, each member of the array (in this case the single member) was sent to arraySum as a simple integer. So the recursive call was the same as the original example: arraySum(1), and the output was the same.

In the case of more numbers, the outputs are all added to the sum and then returned back at the end of the function.

Note that this works quite well for even deeply nested arrays, since arraySum() has logic for arrays that will always return the sum of all of its deeply nested integers:

Resources