As a newbie in the self-teaching learning curve, it's always good to practice coding challenges to stay afresh and test your problem-solving skills. I searched on Codewars and found this challenge and this is what it says:
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
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890"
First Solution
We are going to use the .slice() and .join() array methods. Time to refresh our memories about JavaScript array methods.
The slice() method returns the selected elements in an array as a new array object but this does not change the original array. The slice() method selects elements starting at a given starting argument and ends at, but does not include the end argument.
Syntax
array.slice(start, end)
The join() method returns the array as a string and this method does not change the original array too. The elements will be separated by a specified separator, the default one is a comma(,).
Syntax
array.join(separator)
Solving the challenge
We have an array of numbers separated by commas and they need to look like a phone number. We need to slice to select and group certain numbers together and then join the numbers with a specified separator like this:
numbers.slice(0, 3).join('')
numbers.slice(3, 6).join('')
numbers.slice(6).join('')
We can use string template literals but we have to add parenthesis around the first slice/join method and a dash before the final four numbers
function createPhoneNumber(numbers) {
return `(${numbers.slice(0, 3).join('')}) ${numbers.slice(3, 6).join('')}-${numbers.slice(6).join('')}`;
}
Output
"(123) 456-7890"
Second Solution
We can also create a string at the beginning by joining characters with the join() method and then take specific characters out with the substring() method. The substring() method is somehow similar to the slice() method on arrays. The substring() method extracts characters from a string between two specified indices and returns the new sub-string. It extracts characters from strings between start and end, not including end itself.
function createPhoneNumber(numbers) {
let str = numbers.join('');
return `(${str.substring(0, 3)}) ${str.substring(3, 6)}-${str.substring(6)}`;
}
Output
"(123) 456-7890"
Third Solution
Here we will create format first that will be used to replace numbers using an iteration. The replace function will find and replace the first occurrence of x and then it will move onto the next x in the next iteration and so on.
With a for loop
function createPhoneNumber(numbers) {
let format = "(xxx) xxx-xxxx";
for (let i = 0; i < numbers.length; i++) {
format = format.replace('x', numbers[i]);
}
return format;
}
Output
"(123) 456-7890"
With forEach()
function createPhoneNumber(numbers) {
let format = "(xxx) xxx-xxxx";
numbers.forEach(item => {
format = format.replace('x', item);
});
return format;
}
Output
"(123) 456-7890"
Looking forward to learning about the next challenge.
Happy coding!!