freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-algorithm-scripting/repeat-a-string-repeat-a-st...

2.7 KiB
Raw Blame History

id title isRequired challengeType videoUrl localeTitle
afcc8d540bea9ea2669306b6 Repeat a String Repeat a String true 5 重复一个字符串重复字符串

Description

num times第二个参数重复给定的字符串str (第一个参数)。如果num不是正数,则返回空字符串。如果卡住,请记得使用Read-Search-Ask 。编写自己的代码。

Instructions

Tests

tests:
  - text: '<code>repeatStringNumTimes(&quot;*&quot;, 3)</code>应该返回<code>&quot;***&quot;</code> 。'
    testString: 'assert(repeatStringNumTimes("*", 3) === "***", "<code>repeatStringNumTimes("*", 3)</code> should return <code>"***"</code>.");'
  - text: '<code>repeatStringNumTimes(&quot;abc&quot;, 3)</code>应该返回<code>&quot;abcabcabc&quot;</code> 。'
    testString: 'assert(repeatStringNumTimes("abc", 3) === "abcabcabc", "<code>repeatStringNumTimes("abc", 3)</code> should return <code>"abcabcabc"</code>.");'
  - text: '<code>repeatStringNumTimes(&quot;abc&quot;, 4)</code>应返回<code>&quot;abcabcabcabc&quot;</code> 。'
    testString: 'assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc", "<code>repeatStringNumTimes("abc", 4)</code> should return <code>"abcabcabcabc"</code>.");'
  - text: '<code>repeatStringNumTimes(&quot;abc&quot;, 1)</code>应该返回<code>&quot;abc&quot;</code> 。'
    testString: 'assert(repeatStringNumTimes("abc", 1) === "abc", "<code>repeatStringNumTimes("abc", 1)</code> should return <code>"abc"</code>.");'
  - text: '<code>repeatStringNumTimes(&quot;*&quot;, 8)</code>应该返回<code>&quot;********&quot;</code> 。'
    testString: 'assert(repeatStringNumTimes("*", 8) === "********", "<code>repeatStringNumTimes("*", 8)</code> should return <code>"********"</code>.");'
  - text: '<code>repeatStringNumTimes(&quot;abc&quot;, -2)</code>应返回<code>&quot;&quot;</code> 。'
    testString: 'assert(repeatStringNumTimes("abc", -2) === "", "<code>repeatStringNumTimes("abc", -2)</code> should return <code>""</code>.");'
  - text: 不应使用内置的<code>repeat()</code>方法
    testString: 'assert(!/\.repeat/g.test(code), "The built-in <code>repeat()</code>-method should not be used");'

Challenge Seed

function repeatStringNumTimes(str, num) {
  // repeat after me
  return str;
}

repeatStringNumTimes("abc", 3);

Solution

// solution required