Coding Challenge 01.

Coding Challenge 01.

This will be my first writing about how I solve coding challenges.

The challenge I have done today is -

Create a Phone Number - (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).createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) // => returns "(123) 456-7890".The returned format must be correct to complete this challenge. Don't forget the space after the closing parentheses!

Explanation- So first I am checking what the input is. It's an array. 2nd thing- whats the output - string in the form of a phone number) 3rd thing - condition - I have to follow the format (that is already given-"(123) 456-7890" ).

To get output in the form of string I can use string template literals/template strings which will work to concat multiple parts. Here I am thinking about multiple parts because (see the example) the phone number has three parts. And to concate these three parts I can use Template literals.

Now let's work on the input part. I have an array as an input. The array needs to be divided into three parts. Each of the parts will be a string. I can say

function createPhoneNumber(numbers) {

let firstPart = "";

let secondPart = "";

let thirdPart = "";}

Here to get each part from the array I will use it for loop.

for (let i = 0; i < numbers.length; i++) {
//to extract the first three elements from the array, I can say it needs to be less than three. element 1 whose index position is 0, element 2 index position is 1 & element 3 index position is 2, and element 4 index position is 3. So i < 3.using the if/else condition-
if (i < 3) {
firstPart += numbers[i].toString();
}
//getting each element, converting it into a string then adding up to the variable firstPart.

for the secondPart condition is it needs to greater than & equal to 3. So I will get 4( index position 3, then 5 & six) now to stop here I can put the condition ( i < 6) so

else if (3 <= i && i < 6){
//getting each element, converting into a string then adding up to the variable secondPart.(same as firstPart)
secondPart += numbers[i].toString();
}

finally, to get the third part I just used the else condition thirdPart += numbers[i].toString();

else {
      thirdPart += numbers[i].toString();
    }

now let's see what I have got after putting all the code together.

function createPhoneNumber(numbers) {
let firstPart = "";
let secondPart = "";
let thirdPart = "";
for (let i = 0; i < numbers.length; i++) {
if (i < 3) {firstPart += numbers[i].toString();
} else if (3 <= i && i < 6) {secondPart += numbers[i].toString();
} else {thirdPart += numbers[i].toString();
}}}

Finally, I need to return the variables & join them outside the loop. To do that I will put

return (${firstPart}) ${secondPart}-${thirdPart};

before the firstPart variable I have added (), then space, secondPart, then - and thirdPart. In the beginning, I mentioned using Template literals to join multiple strings together. The final solution is -

function createPhoneNumber(numbers) {
let firstPart = "";
let secondPart = "";
let thirdPart = "";
for (let i = 0; i < numbers.length; i++) {
if (i < 3) {firstPart += numbers[i].toString();
} else if (3 <= i && i < 6) {secondPart += numbers[i].toString();
} else {thirdPart += numbers[i].toString();
}}
return (${firstPart}) ${secondPart}-${thirdPart};
}