DEV Community

loading...

Interview question for a Senior Js Developer, pt 1 (Questions), updated with answers link.

eulier profile image Eulier Gonzalez ใƒป3 min read

I applied for a company using ApplyByAPI

Here in my blog i described how i did it.

After the process they email me and provide me a couples of questions for an interview.

I'm doing it, just for educational purpose to highlight the common concepts that you'll need to know as a senior js developer.
In this entry of my blog i explain a quick review over the fundamentals, that i learned from Zero To Mastery Academy

If you wanna know my answers

JS Developer Coding Exemplar

Instructions

This coding exemplar is designed to highlight your skills and areas of expertise with the JS language in general. We rely mainly on JS as our front-end technology of choice. Because of this, it's important that developers have a well-rounded understanding of the JS language and its newest specifications (ES6/ES7).

Please complete the following questions to the best of your ability. If you are unable to solve a question, please indicate as such. You should feel free to use any online resources to solve these questions; after all, we expect that our developers will use their problem-solving skills at work! Some questions are intended to be difficult, while others are meant to be easy or obvious. Please post your answers in a Gist, using Markdown format, and send the link for review.

This exercise should take approximately one hour to complete.

Good luck!

Question 1

You've been tasked with identifying a string that contains the word "superman" (case insensitive). You've written the following code:

function validateString(str) {
    if (!str.toLowerCase().indexOf('superman')) {
        throw new Error('String does not contain superman');
    }    
}
Enter fullscreen mode Exit fullscreen mode

QA has come to you and said that this works great for strings like "I love superman", but an exception is generated for strings like "Superman is awesome!", which should not happen. Explain why this occurs, and show how you would solve this issue (you must use indexOf() in your answer).

Question 2

You're given a sorted index array that contains no keys. The array contains only integers, and your task is to identify whether or not the integer you're looking for is in the array. Write a function that searches for the integer and returns true or false based on whether the integer is present. Describe how you arrived at your solution.

Question 3

Write a function that takes a phone number in any form and formats it using a delimiter supplied by the developer. The delimiter is optional; if one is not supplied, use a dash (-). Your function should accept a phone number in any format (e.g. 123-456-7890, (123) 456-7890, 1234567890, etc) and format it according to the 3-3-4 US block standard, using the delimiter specified. Assume foreign phone numbers and country codes are out of scope.

Note: This question CAN be solved using a regular expression, but one is not REQUIRED as a solution. Focus instead on cleanliness and effectiveness of the code, and take into account phone numbers that may not pass a sanity check.

Question 4

Write a complete set of unit tests for the following code:


function fizzBuzz(start = 1, stop = 100)
{
    let result = '';

    if (stop < start || start < 0 || stop < 0) {
        throw new Error('Invalid arguments');
    }

    for (let i = start; i <= stop; i++) {
        if (i % 3 === 0 && i % 5 === 0) {
            result += 'FizzBuzz';
            continue;
        }

        if (i % 3 === 0) {
            result += 'Fizz';
            continue;
        }

        if (i % 5 === 0) {
            result += 'Buzz';
            continue;
        }

        result += i;
    }

    return result;
}
Enter fullscreen mode Exit fullscreen mode

Question 5

Write a function that would generate a hex color code (#f1f2f3) from the full name of a person. It should always generate the same color for a given name. Describe how you arrived at your solution.

const name = 'John Doe';
const color = getColorFromName(name); // e.g. #9bc44c
Enter fullscreen mode Exit fullscreen mode

Question 6

Considering the following ES5 code in a page that has ten buttons:

(function(){
    for (var i = 0, l = 10; i < l; i++) {
        document.getElementById('button-' + i).onclick = function () {
            console.log('Line %s', i);
        };
    }
})();
Enter fullscreen mode Exit fullscreen mode

What is the bug in this code? Explain how to fix it in plain ES5.

Question 7

Write a function that determines if a given argument is array-like, in the sense that it is iterable.

isIterable(null); // false
isIterable('hello world'); // true
isIterable(document.querySelectorAll('.error-message')); // true
Enter fullscreen mode Exit fullscreen mode

Discussion (3)

pic
Editor guide
Collapse
parkadzedev profile image
Michael Parkadze

Correct me if I am wrong but for the first question the answer is to change the condition to !== -1 as indexOf returns the starting point of the substring that was found.

So I think because โ€˜super is awesomeโ€™ returns an error is because it returns a 0 and it might not consider it in the condition to be true.

Collapse
scottbromander profile image
Scott Bromander

You are correct. The case breaks because superman in the second case starts at the first position, array index 0.

You would have to do some other shifting as well to make a !== work. !str.toLowerCase().indexOf('superman') is technically false. So false !== -1 is actually true. But false !== 7 is also true. A good way to approach it if you wanted to use equality would be something like str.toLowerCase().indexOf('superman') === -1 ) for the condition.

But generally, I favor less characters, so something like:
if (str.toLowerCase().indexOf('superman') < 0).

But, as always, shred my solution! I love feedback. Only way to grow is to learn from others who are more right!

Collapse
eulier profile image