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:

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

思路

答案

1
2
3
function createPhoneNumber(numbers) {
return "(" + numbers[0] + numbers[1] + numbers[2] + ") " + numbers[3] + numbers[4] + numbers[5] + "-" + numbers[6] + numbers[7] + numbers[8] + numbers[9];
}

Your order, please

题目

Your task is to sort a given string. Each word in the string will contain a single number. This number is the position the word should have in the result.

Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).

If the input string is empty, return an empty string. The words in the input String will only contain valid consecutive numbers.

Examples

1
2
3
"is2 Thi1s T4est 3a"  -->  "Thi1s is2 3a T4est"
"4of Fo1r pe6ople g3ood th5e the2" --> "Fo1r the2 g3ood 4of th5e pe6ople"
"" --> ""

思路

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
function order(words) {
var arr = words.split(" ");
var w_obj = new Object();
var r_string = new String();
for (var i in arr) {
var n = arr[i].match(/[1-9]/g);
w_obj[n] = arr[i];
}
for (var i = 1; i <= Object.getOwnPropertyNames(w_obj).length; i++) {
r_string = r_string + w_obj[i] + " "
}
return r_string.trim();
}