admin管理员组

文章数量:1410717

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 plete 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])

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 plete 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])
Share Improve this question asked May 14, 2020 at 22:36 Charlie VdbCharlie Vdb 431 gold badge1 silver badge11 bronze badges 2
  • Forgot i == 3? Maybe you better can describe what result you got incorrect? And is it valid syntax? Don't you need else if (3 <= i && i < 6)? – Justinas Commented May 14, 2020 at 22:38
  • Missing ) in the format. Did nobody spot this? – Tvde1 Commented May 2, 2022 at 11:42
Add a ment  | 

9 Answers 9

Reset to default 2

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 {
}

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})`
}    
function createPhoneNumber(numbers){
  var format = "(xxx) xxx-xxxx";
  
  for(var i = 0; i < numbers.length; i++)
  {
    format = format.replace('x', numbers[i]);
  }
  
  return format;
}
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;
}
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])

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}`
} 

This JavaScript function takes an array of 10 digits and converts it into a well-formatted phone number string in the format (XXX) XXX-XXXX. It does this by joining the digits into a single string and then breaking that string into the required segments using the substring method and concatenating them together with the appropriate characters.

function createPhoneNumber(numbers){
  numbers = numbers.join('');
  return '(' + numbers.substring(0, 3) + ') ' 
      + numbers.substring(3, 6) 
      + '-' 
      + numbers.substring(6);
}

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}`
} 

const createPhoneNumber = function (arr) {
  const number = `(${arr.slice(0, 3).join('')}) ${arr
    .slice(3, 6)
    .join('')}-${arr.slice(7).join('')}`;
  console.log(number);
};

createPhoneNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);

本文标签: javascriptCreate Phone Number coding challengeStack Overflow