Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const test_array = [2,1,2,5,5,5,0,1,2,3];
- //using for loop
- function format_phone_number(array)
- {
- let result = "";
- for (i in array)
- {
- if (i == 0)
- {
- result += "(";
- }
- if (i == 6)
- {
- result += "-";
- }
- result += array[i];
- if (i == 2)
- {
- result += ") "
- }
- }
- console.log(result);
- return result;
- }
- //using slice and join
- function format_phone_number(array)
- {
- first = array.slice(0,3);
- second = array.slice(3,6);
- third = array.slice(6);
- result = "(" + first.join("") + ") " + second.join("") + "-" + third.join("");
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement