-
[Codewars] Create Phone NumberAlgorithm 2020. 10. 27. 19:18
[문제] Create Phone Number
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"
수도코드
1. 처음 숫자 3개를 배열에서 잘라내고 문자열로 바꾸어 새로운 변수에 할당 2. 나머지 숫자 3개, 4개도 배열에서 잘라내고 문자열로 바꾸어 새로운 변수에 각각 할당 3. 백틱(`)을 사용하여 위 변수를 전화번호 형식에 맞게 넣어 리턴
코드 구현
function createPhoneNumber(numbers){ let first = numbers.slice(0, 3).join(''); let second = numbers.slice(3, 6).join(''); let third = numbers.slice(6, 10).join(''); return `(${first}) ${second}-${third}`; }
'Algorithm' 카테고리의 다른 글
[프로그래머스] 약수의 합 (0) 2021.01.18 [Codewars] Valid Braces (0) 2020.11.16 [Codewars] Format a string of names like 'Bart, Lisa & Maggie' (0) 2020.11.08 [Codewars] Are they the "same"? (0) 2020.11.08 [Codewars] Who likes it? (0) 2020.10.28