7.5 KiB
Raw Blame History

id, title, challengeType, isRequired, videoUrl, localeTitle
id title challengeType isRequired videoUrl localeTitle
aff0395860f5d3034dc0bfc9 Telephone Number Validator 5 true 电话号码验证器

Description

如果传递的字符串看起来像有效的美国电话号码,则返回true 。用户可以按照他们选择的方式填写表单字段,只要其具有有效美国号码的格式即可。以下是美国数字的有效格式示例(有关其他变体,请参阅下面的测试):
555-555-5555
(555)555-5555
555555-5555
555 555 5555
5555555555
1 555 555 5555
对于此挑战,您将看到一个字符串,如800-692-77538oo-six427676;laskdjf 。您的工作是根据上面提供的任何格式组合验证或拒绝美国电话号码。区号是必需的。如果提供了国家/地区代码,则必须确认国家/地区代码为1 。如果字符串是有效的美国电话号码,则返回true ;否则返回false 。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。

Instructions

Tests

tests:
  - text: <code>telephoneCheck(&quot;555-555-5555&quot;)</code>应该返回一个布尔值。
    testString: 'assert(typeof telephoneCheck("555-555-5555") === "boolean", "<code>telephoneCheck("555-555-5555")</code> should return a boolean.");'
  - text: <code>telephoneCheck(&quot;1 555-555-5555&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("1 555-555-5555") === true, "<code>telephoneCheck("1 555-555-5555")</code> should return true.");'
  - text: <code>telephoneCheck(&quot;1 (555) 555-5555&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("1 (555) 555-5555") === true, "<code>telephoneCheck("1 (555) 555-5555")</code> should return true.");'
  - text: <code>telephoneCheck(&quot;5555555555&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("5555555555") === true, "<code>telephoneCheck("5555555555")</code> should return true.");'
  - text: <code>telephoneCheck(&quot;555-555-5555&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("555-555-5555") === true, "<code>telephoneCheck("555-555-5555")</code> should return true.");'
  - text: <code>telephoneCheck(&quot;(555)555-5555&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("(555)555-5555") === true, "<code>telephoneCheck("(555)555-5555")</code> should return true.");'
  - text: <code>telephoneCheck(&quot;1(555)555-5555&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("1(555)555-5555") === true, "<code>telephoneCheck("1(555)555-5555")</code> should return true.");'
  - text: <code>telephoneCheck(&quot;555-5555&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("555-5555") === false, "<code>telephoneCheck("555-5555")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;5555555&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("5555555") === false, "<code>telephoneCheck("5555555")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;1 555)555-5555&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("1 555)555-5555") === false, "<code>telephoneCheck("1 555)555-5555")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;1 555 555 5555&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("1 555 555 5555") === true, "<code>telephoneCheck("1 555 555 5555")</code> should return true.");'
  - text: <code>telephoneCheck(&quot;1 456 789 4444&quot;)</code>应该返回true。
    testString: 'assert(telephoneCheck("1 456 789 4444") === true, "<code>telephoneCheck("1 456 789 4444")</code> should return true.");'
  - text: '<code>telephoneCheck(&quot;123**&amp;!!asdf#&quot;)</code>应该返回false。'
    testString: 'assert(telephoneCheck("123**&!!asdf#") === false, "<code>telephoneCheck("123**&!!asdf#")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;55555555&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("55555555") === false, "<code>telephoneCheck("55555555")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;(6054756961)&quot;)</code>应该返回false
    testString: 'assert(telephoneCheck("(6054756961)") === false, "<code>telephoneCheck("(6054756961)")</code> should return false");'
  - text: <code>telephoneCheck(&quot;2 (757) 622-7382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("2 (757) 622-7382") === false, "<code>telephoneCheck("2 (757) 622-7382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;0 (757) 622-7382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("0 (757) 622-7382") === false, "<code>telephoneCheck("0 (757) 622-7382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;-1 (757) 622-7382&quot;)</code>应该返回false
    testString: 'assert(telephoneCheck("-1 (757) 622-7382") === false, "<code>telephoneCheck("-1 (757) 622-7382")</code> should return false");'
  - text: <code>telephoneCheck(&quot;2 757 622-7382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("2 757 622-7382") === false, "<code>telephoneCheck("2 757 622-7382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;10 (757) 622-7382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("10 (757) 622-7382") === false, "<code>telephoneCheck("10 (757) 622-7382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;27576227382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("27576227382") === false, "<code>telephoneCheck("27576227382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;(275)76227382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("(275)76227382") === false, "<code>telephoneCheck("(275)76227382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;2(757)6227382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("2(757)6227382") === false, "<code>telephoneCheck("2(757)6227382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;2(757)622-7382&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("2(757)622-7382") === false, "<code>telephoneCheck("2(757)622-7382")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;555)-555-5555&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("555)-555-5555") === false, "<code>telephoneCheck("555)-555-5555")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;(555-555-5555&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("(555-555-5555") === false, "<code>telephoneCheck("(555-555-5555")</code> should return false.");'
  - text: <code>telephoneCheck(&quot;(555)5(55?)-5555&quot;)</code>应该返回false。
    testString: 'assert(telephoneCheck("(555)5(55?)-5555") === false, "<code>telephoneCheck("(555)5(55?)-5555")</code> should return false.");'

Challenge Seed

function telephoneCheck(str) {
  // Good luck!
  return true;
}

telephoneCheck("555-555-5555");

Solution

// solution required