Create the normalizePhoneNumber
function that takes a string representing a ten-digit number and returns it formatted in a specific phone number format.
function normalizePhoneNumber(num)
- num: A string representing a ten-digit phone number.
- Format the input string
num
into the phone number format: "(XXX) XXX-XXXX". - You can assume that all entries will be exactly 10 digits long.
let result = normalizePhoneNumber("9876543210");
console.log(result);
Expected Output:
"(987) 654-3210"
let result = normalizePhoneNumber("1111111111");
console.log(result);
Expected Output:
"(111) 111-1111"
- Ensure that the function accurately checks for the length of the input string and validates each character.
- Consider using a template "(XXX) XXX-XXXX" and loop backwards, replacing 'X' with digits from the input number.