diff --git a/String/FormatPhoneNumber.js b/String/FormatPhoneNumber.js index c1bbde8320..0a6fe6fc5b 100644 --- a/String/FormatPhoneNumber.js +++ b/String/FormatPhoneNumber.js @@ -1,17 +1,16 @@ -// function that takes 10 digits and returns a string of the formatted phone number -// e.g.: 1234567890 -> (123) 456-7890 - -const formatPhoneNumber = (numbers) => { - const numbersString = numbers.toString() - if ((numbersString.length !== 10) || isNaN(numbersString)) { +/** + * @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 + */ +const formatPhoneNumber = (phoneNumber) => { + if ((phoneNumber.length !== 10) || isNaN(phoneNumber)) { // return "Invalid phone number." - throw new TypeError('Invalid phone number.') + throw new TypeError('Invalid phone number!') } - const arr = '(XXX) XXX-XXXX'.split('') - Array.from(numbersString).forEach(n => { - arr[arr.indexOf('X')] = n - }) - return arr.join('') + + let index = 0 + return '(XXX) XXX-XXXX'.replace(/X/g, () => phoneNumber[index++]) } -export { formatPhoneNumber } +export default formatPhoneNumber diff --git a/String/test/FormatPhoneNumber.test.js b/String/test/FormatPhoneNumber.test.js index 0e9febcb79..42ae24a8a9 100644 --- a/String/test/FormatPhoneNumber.test.js +++ b/String/test/FormatPhoneNumber.test.js @@ -1,23 +1,15 @@ -import { formatPhoneNumber } from '../FormatPhoneNumber' - -describe('PhoneNumberFormatting', () => { - it('expects to return the formatted phone number', () => { - expect(formatPhoneNumber('1234567890')).toEqual('(123) 456-7890') - }) - - it('expects to return the formatted phone number', () => { - expect(formatPhoneNumber(1234567890)).toEqual('(123) 456-7890') - }) +import formatPhoneNumber from '../FormatPhoneNumber' +describe('Testing the formatPhoneNumber functions', () => { it('expects to throw a type error', () => { - expect(() => { formatPhoneNumber('1234567') }).toThrow('Invalid phone number.') + expect(() => formatPhoneNumber('1234567')).toThrow('Invalid phone number!') + expect(() => formatPhoneNumber('123456text')).toThrow('Invalid phone number!') + expect(() => formatPhoneNumber(12345)).toThrow('Invalid phone number!') }) - it('expects to throw a type error', () => { - expect(() => { formatPhoneNumber('123456text') }).toThrow('Invalid phone number.') - }) - - it('expects to throw a type error', () => { - expect(() => { formatPhoneNumber(12345) }).toThrow('Invalid phone number.') + it('expects to return the formatted phone number', () => { + expect(formatPhoneNumber('1234567890')).toEqual('(123) 456-7890') + expect(formatPhoneNumber('2124323322')).toEqual('(212) 432-3322') + expect(formatPhoneNumber('1721543455')).toEqual('(172) 154-3455') }) })
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: