Create Phone Number


DESCRIPTION

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!

SOLUTION

  • 方法一:数组的 slice() 方法
1
2
3
4
5
6
7
8
9
10
11
function createPhoneNumber(numbers) {
// Create an array of the different parts of the phone number
const areaCode = `(${numbers.slice(0, 3).join("")})`;
const prefix = numbers.slice(3, 6).join("");
const suffix = numbers.slice(6, 10).join("");

// Combine the parts into the final phone number string
const phoneNumber = `${areaCode} ${prefix}-${suffix}`;

return phoneNumber;
}
  • 方法二:字符串的 replace() 方法
1
2
3
4
5
6
7
8
9
function createPhoneNumber(numbers) {
var format = "(xxx) xxx-xxxx";

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

return format;
}
  • 方法三:正则表达式
1
2
3
function createPhoneNumber(numbers) {
return numbers.join("").replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
}

Reference

Create Phone Number