Create Phone Number ( 6 kyu )

문제

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

주어진 배열로 형식에 맞게 문자열로 리턴

풀이

function createPhoneNumber(numbers){
  return `(${numbers.slice(0,3).join('')}) ${numbers.slice(3,6).join('')}-${numbers.slice(6, 10).join('')}`;
}

slice, join으로해서 작성

다른 사람의 풀이

function createPhoneNumber(numbers){
  var format = "(xxx) xxx-xxxx";

  for(var i = 0; i < numbers.length; i++)
  {
    format = format.replace('x', numbers[i]);
  }

  return format;
}

replace로 할수가 있는건 신기하네


'자료구조, 알고리즘 > Codewars 문제' 카테고리의 다른 글

Sort the odd ( 6 kyu )  (0) 2017.12.06
Is a number prime? ( 6 kyu )  (0) 2017.12.06
Convert string to camel case ( 5 kyu )  (0) 2017.12.05
Human Readable Time ( 5 kyu )  (0) 2017.12.05
Simple Pig Latin ( 5 kyu )  (0) 2017.12.05

+ Recent posts