DEV Community

Cover image for Create Phone Number using JS , Codewars Solution
preyas prathap
preyas prathap

Posted on

Create Phone Number using JS , Codewars Solution

Recently I've been trying out some questions for improving my JS Skills. So I thought I will document some of that here.

Codewars Link : https://www.codewars.com/kata/525f50e3b73515a6db000b83

Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.

Example

createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"

My Solution

function createPhoneNumber(numbers){
return numbers.join('').replace(/(...)(...)(.*)/,'($1) $2-$3');
}

The Regex Part

/ / - tells that it's a regex
(...) - substring that contains 3 numbers
(.*) - extra numbers

Top comments (0)