Create Phone Number

JavaScript

function createPhoneNumber(numbers) {
  return `(${numbers.getRangeAsString(0,2)}) ${numbers.getRangeAsString(3,5)}-${numbers.getRangeAsString(6,9)}`;
}

Array.prototype.getRangeAsString = function(start, stop) {
  var target = this, string = "";
  for (let index = start; index <= stop; index++) {
    string += target[index];
  }

  return string;
};

PHP

function createPhoneNumber($numbersArray) {
  $numbers = implode("", $numbersArray);
  return "(" . substr($numbers,0,3) . ") " . substr($numbers,3,3) . "-" . substr($numbers,6,4);
}

Tags

  1. javascript (Private)
  2. php (Private)
  3. 6-kyu (Private)
  4. codewars (Private)
  5. answer (Private)