알고리즘 36 - Create Phone Number

박진현·2021년 7월 19일
0

Q.

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"
The returned format must be correct in order to complete this challenge.
Don't forget the space after the closing parentheses!

A)

function createPhoneNumber(numbers){
  let front = numbers.slice(0,3)
  front.push(')')
  front.unshift('(')
  let middle = numbers.slice(3,6)
  middle.unshift(' ')
  let end = numbers.slice(6,10)
  end.unshift('-')
  
  return [...front,...middle,...end].join('')
}
profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글