## DEV Community is a community of 601,436 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

loading...

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

## Discussion (10)  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('');
};
`````` 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
`````` 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
`````` 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();
}
`````` 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]))
`````` 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]))
`````` 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));
}
``````