DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #178 - Create Phone Numbers

Setup

Implement a function that accepts an array of 10 integers. Have the function return those numbers as a String in the form of a phone number. Please use the following format for the phone number: (XXX) XXX-XXXX. Integers in the array will be no larger than 9.

Example

createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // should return (123) 456-7890

Tests

createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
createPhoneNumber([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])

Good luck!


This challenge comes from xDranik on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (10)

Collapse
 
jehielmartinez profile image
Jehiel Martinez
function createPhoneNumber(arr) {
    if(arr.length < 10) {
        return 'Your phone number is not from this country'
    }

    arr.splice(0,0, '(')
    arr.splice(4,0, ')')
    arr.splice(5,0, ' ')
    arr.splice(9,0, '-')

    return arr.join('');
};
Collapse
 
savagepixie profile image
SavagePixie

Elixir

I'm sure there's a much simpler way, though.

defmodule Phone do
  def get_number([ a, b, c, d, e, f, g, h, i, j ]) do
    "(#{a}#{b}#{c}) #{d}#{e}#{f}-#{g}#{h}#{i}#{j}"
  end
end
Collapse
 
craigmc08 profile image
Craig McIlwrath

Haskell

createPhoneNumber :: [Int] -> String
createPhoneNumber ns = let a = concatMap show $ take 3 ns
                           b = concatMap show $ take 3 $ drop 3 ns
                           c = concatMap show $ take 4 $ drop 6 ns
                       in  '(' : a ++ ") " ++ b ++ "-" ++ c
Collapse
 
epranka profile image
Edvinas Pranka
function createPhoneNumber(n) {
   return `({n[0]}{n[1]}{n[2]}) {n[3]}{n[4]}{n[5]}-{n[6]}{n[7]}{n[8]}{n[9]}`;
}
Collapse
 
cipharius profile image
Valts Liepiņš

Ruby

def createPhoneNumber d
    "(#{d[0..2].join}) #{d[3..5].join}-#{d[6..9].join}"
end
Collapse
 
exts profile image
Lamonte • Edited

Dart - I'm all about being explicit and ensuring the expected result is what I want rather than trying to be creative and smart.

String createPhoneNumber(List<int> numbers) {
  numbers = numbers.takeWhile((num) => num < 10).toList();
  if(numbers.length < 10) return null;
  var result = StringBuffer("(");
  for(var idx = 0; idx < numbers.length; idx++) {
    result.write(numbers[idx]);
    switch(idx) {
      case 2: 
        result.write(") ");
        break;
      case 5:
        result.write("-");
        break;
    }
  }
  return result.toString();
}
Collapse
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑
def create_phone_number(nums):
  strnums = [str(i) for i in nums]
  return "(%s) %s-%s" % (
    ''.join(strnums[0:3]),
    ''.join(strnums[3:6]),
    ''.join(strnums[6:10]))

print(create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]))
Collapse
 
kerldev profile image
Kyle Jones

In Python:

def createPhoneNumber(numbers):
    numbers = [str(i) for i in numbers]
    return "({}) {}-{}".format(''.join(numbers[:3]), ''.join(numbers[3:6]), ''.join(numbers[6:10]))


print(createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]))
print(createPhoneNumber([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
Collapse
 
mathanagopal97 profile image
Mathanagopal Sankarasubramanian

JAVA

    public static String getNumbers(int[] arr, int start, int end){
        String number = "";
        for(int i=start;i<=end;i++){
            if((""+arr[i]).length()>=2) {
                System.out.println("Let all the numbers be of single digit (Number at position "+i+").");
                System.exit(1);
            }
            number = number+arr[i];
        }
        return number;
    }

    public static String generateNumbers(int[] arr){
        if(arr.length<0) System.out.println("Not valid");
        else{
            String phoneNumber = "("+getNumbers(arr,0,2)+")"+getNumbers(arr,3,5)+"-"+getNumbers(arr,6,arr.length-1);
            return phoneNumber;
        }
        return "";
    }
    public static void main(String[] args) {
        int[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        System.out.println(generateNumbers(arr));
    }
Collapse
 
aminnairi profile image
Amin

JavaScript

const createPhoneNumber = ([a, b, c, d, e, f, g, h, i, j]) => `(${a}${b}${c}) ${d}${e}${f}-${g}${h}${i}${j}`;