'Create Phone Number coding challenge
Can someone explain me what wrong with my code below?
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" The returned format must be correct in order to complete this challenge. Don't forget the space after the closing parentheses!
function createPhoneNumber(numbers){
let firstpart = ""
let secondpart = ""
let thirdpart = ""
for(var i=0;i<numbers.length;i++){
if(i<3){
firstpart.concat(numbers[i].toString())
} else if(3<i<6){
secondpart.concat(numbers[i].toString())
} else if(i>=6){
thirdpart.concat(numbers[i].toString())
}
}
return `(${firstpart} ${secondpart}-${thirdpart}`
}
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
Solution 1:[1]
Concat returns the result of the string and the added string, it does not change the original string. You want firstpart += numbers[i].toString
or firststring = firststring.concat(numbers[1].toString)
The other answer is also correct. Comparisons in the form of a < b < c may not work as expected. If you need to check that a is less than b and b is less than c, do a < b && b < c
. In your case, the check that the number is less than 3 is redundant. If it were less than 3, the first block would have caught it, so you if statement should be in the form of:
if (i < 3) {
}
else if (i < 6) {
}
else {
}
Solution 2:[2]
First error
firstpart.concat(numbers[i].toString())
This statement (as well as the others) just doesn't behave as you expect. The function concat does not modify the object, it just returns the string obtained by concatenating the two strings. Hence, firstpart never gets modified. Try this instead:
firstpart += numbers[i].toString()
Second error:
3<i<6
This is interpreted like so:
- (3 < i) < 6
- (true or false) < 6
- 1 (or 0) < 6
- true
Ternary boolean expressions won't work in JS. This should fix the issue:
i >= 3 && i < 6
Solution
function createPhoneNumber(numbers) {
let firstpart = ""
let secondpart = ""
let thirdpart = ""
for (var i = 0; i < numbers.length; i++) {
if (i < 3) {
firstpart += numbers[i].toString()
} else if (i >= 3 && i < 6) {
secondpart += numbers[i].toString()
} else if (i >= 6) {
thirdpart += numbers[i].toString()
}
}
return `(${firstpart} ${secondpart}-${thirdpart})`
}
Solution 3:[3]
function createPhoneNumber(numbers){
var format = "(xxx) xxx-xxxx";
for(var i = 0; i < numbers.length; i++)
{
format = format.replace('x', numbers[i]);
}
return format;
}
Solution 4:[4]
function createPhoneNumber(numbers)
{
let conCode = numbers.slice(0,3);
country = conCode.join('');
let areaCode = numbers.slice(3,6);
area = areaCode.join('');
let postCode = numbers.slice(-4);
post = postCode.join('');
return "(" + country + ")" + " " + area + "-" + post;
}
Solution 5:[5]
function createPhoneNumber(numbers){
let fPart='', sPart='', lPart='';
for(let i =0; i<numbers.length; i++){
if(i < 3){
fPart = `${fPart}${numbers[i]}`;
}
if(i >= 3 && i < 6){
sPart = `${sPart}${numbers[i]}`;
}
if(i >= 6 && i < 10){
lPart = `${lPart}${numbers[i]}`;
}
}
return `(${fPart}) ${sPart}-${lPart}`;
}
createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Community |
Solution 3 | shamila |
Solution 4 | jaynesh |
Solution 5 | Mrkouhadi |