...

/

Solution Review: Create Phone Number

Solution Review: Create Phone Number

This lesson will explain the solution to the problem in the previous lesson.

We'll cover the following...

There are various ways to solve the problem in the previous lesson. However, the question required the use of slice to solve the problem. Let’s discuss the solution now.

Solution #

Press + to interact
function createPhoneNumber(arr) {
return `(${arr.slice(0, 3).join('')}) ${arr.slice(3, 6).join('')}-${arr.slice(6).join('')}`;
}
console.log(createPhoneNumber([1,2,3,4,5,6,7,8,9,0]))
console.log(createPhoneNumber([3,5,7,1,2,0,9,8,6,4]))

Explanation #

This is an obvious solution. To take out the values from specific indexes, we make use of the .slice method. To convert them to ...