DEV Community

Cesar Del rio
Cesar Del rio

Posted on β€’ Edited on

1 1

#24 - Create Phone Number CodeWars Kata (6 kyu)

Instructions

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

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

The returned format must be correct in order to complete this challenge.
Don't forget the space after the closing parentheses!


My solution:

return '(' + n[0] + n[1] + n[2] +')' + ' ' + n[3] + n[4] + n[5] + '-' + n[6] + n[7] + n[8] + n[9]
Enter fullscreen mode Exit fullscreen mode

Explanation

I just used the index of every number, and joined it with the elements that I need to add for getting the correct string


What do you think about this solution? πŸ‘‡πŸ€”

My Github
My twitter
Solve this Kata

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ β€’
const makePhoneNumber = a=>[...'(012) 345-6789'].map(c=>a[c]||c).join('')
Enter fullscreen mode Exit fullscreen mode
Collapse
 
cesar__dlr profile image
Cesar Del rio β€’

Nice solution πŸ”₯

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay