forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatPhoneNumber.js
More file actions
Latest commit
16 lines (14 loc) · 543 Bytes
/
Copy pathFormatPhoneNumber.js
File metadata and controls
16 lines (14 loc) · 543 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @description - function that takes 10 digits and returns a string of the formatted phone number e.g.: 1234567890 -> (123) 456-7890
* @param {string} phoneNumber
* @returns {string} - Format to (XXX) XXX-XXXX pattern
*/
constformatPhoneNumber=(phoneNumber)=>{
if(phoneNumber.length!==10||isNaN(phoneNumber)){
// return "Invalid phone number."
thrownewTypeError('Invalid phone number!')
}
letindex=0
return'(XXX) XXX-XXXX'.replace(/X/g,()=>phoneNumber[index++])
}
exportdefaultformatPhoneNumber