Interview question for a Senior Js Developer, pt 1 (Questions), updated with answers link.
Tags: javascript, webdev, node, devjournal
Published: 2021-01-15 21:31:27

I applied for a company using [ApplyByAPI](https://applybyapi.com/)

Here in my blog i [described](https://gallant-bell-850d88.netlify.app/2020/november/how-to-applybyapi.html#the-process) 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](
https://gallant-bell-850d88.netlify.app/2020/november/learned-2-courses-from-zero-to-mastery.html#js-advanced-concepts), that i learned from [Zero To Mastery Academy](https://academy.zerotomastery.io/p/advanced-javascript-concepts)

If you wanna know my [answers](https://dev.to/eulier/interview-question-for-a-senior-js-developer-final-part-answer-2bmn)

# 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:
```js
function validateString(str) {
    if (!str.toLowerCase().indexOf('superman')) {
        throw new Error('String does not contain superman');
    }    
}
```

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:

```js

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;
}
```

## 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.

```js
const name = 'John Doe';
const color = getColorFromName(name); // e.g. #9bc44c
```

## Question 6

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

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

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.

```js
isIterable(null); // false
isIterable('hello world'); // true
isIterable(document.querySelectorAll('.error-message')); // true
```