I got rejected in tech screen yesterday. I was asked to write a JavaScript function and send it over email. I have also added hiring manager's response at the bottom.
Could you please write a JavaScript function to compare two strings that look like phone numbers and indicate whether the digits in both strings match? (To be clear, while the input looks like phone numbers, this question doesn't care how phones/telephony/dialing works.) Your code should compare only the digits of the input, ignore any letters or punctuation, and ignore the fact that country/area codes can be optional. As an example, "(123) 456-7890" and "123.456.7890" match because both have the digits "1234567890" in that order, while "1-123-456-7890" and "123-456-7890" do not match (even though they'll probably reach the same person if dialed in the US).
I wasn't sure if regex would be an acceptable solution so I implemented it two ways. Please, let me know what I could have done better.
"use strict"
var CompareNumbers = function () {
//#region private variables
var _phoneNumber1;
var _phoneNumber2;
var _minPhoneLength = 1;
var _validateResult;
var _notValidMessage = "Not a valid Phone Number";
var _successMessage = "Match";
var _failureMessage = "Not a Match";
var _inputArray1;
var _inputArray2;
var _numArray1;
var _numArray2;
//#endregion
//#region private functions
//function using regex to remove spaces & braces etc
var _compNumbersUsingRegex = function (input1, input2) {
if (_validateInputs(input1, input2)) {
_phoneNumber1 = input1.replace(/[^0-9]+/g, '');
_phoneNumber2 = input2.replace(/[^0-9]+/g, '');
if (_validateInputs(_phoneNumber1, _phoneNumber2)) {
if (_phoneNumber1 == _phoneNumber2) {
return _successMessage;
}
else {
return _failureMessage;
}
}
return _notValidMessage;
}
else {
return _notValidMessage;
}
}
//function using arrays for comparison
var _compNumbersUsingArray = function (input1, input2) {
if (_validateInputs(input1, input2)) {
_inputArray1 = new Array();
_inputArray2 = new Array();
_inputArray1 = input1.split('');
_numArray1 = new Array();
for (var i = 0; i < _inputArray1.length; i++) {
if (!isNaN(_inputArray1[i]) && _inputArray1[i] != ' ') {
_numArray1.push(_inputArray1[i]);
}
}
_inputArray2 = input2.split('');
_numArray2 = new Array();
for (var i = 0; i < _inputArray2.length; i++) {
if (!isNaN(_inputArray2[i]) && _inputArray2[i] != ' ') {
_numArray2.push(_inputArray2[i]);
}
}
if (_validateInputs()) {
return _numArray1.toString() == _numArray2.toString() ? _successMessage : _failureMessage;
}
else {
return _notValidMessage;
}
}
else {
return _notValidMessage;
}
}
//Helper funciton to validate if inputs exceed a minimum number declared in variable section
function _validateInputs(validate1, validate2) {
if (validate1.length < _minPhoneLength || validate2.length < _minPhoneLength) {
return false;
}
else {
return true;
}
}
//#endregion
//#region public members
var publics = {
comparePhoneNumbers1: _compNumbersUsingArray,
comparePhoneNumbers2: _compNumbersUsingRegex
};
return publics;
//#endregion
}();
The response that I got back from the hiring manager was:
Thanks! I've looked over your code and think you captured the essence of the problem correctly. I like that you included both approaches - I prefer the _compNumbersUsingRegex implementation which is basically the one I like to see (regex+compare). That said, your resume shows you've been working with JavaScript in your current role, but some of the patterns I see in your code aren't quite in line with what I look for from senior developers on the team, so this role might not be the perfect match for you.
#region
, which is a .NET thing and should never ever be in JS. Well, one could argue it shouldn't ever exist anywhere. \$\endgroup\$