Oliver Eyton-Williams f1c9b08cf3 fix(curriculum): add isHidden: false to challenges
This includes certificates (where it does nothing), but does not
include any translations.
2020-05-25 16:25:19 +05:30

2.1 KiB

id, title, isRequired, challengeType, isHidden, forumTopicId
id title isRequired challengeType isHidden forumTopicId
afcc8d540bea9ea2669306b6 Repeat a String Repeat a String true 5 false 16041

Description

Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number.

Instructions

Tests

tests:
  - text: <code>repeatStringNumTimes("*", 3)</code> should return <code>"***"</code>.
    testString: assert(repeatStringNumTimes("*", 3) === "***");
  - text: <code>repeatStringNumTimes("abc", 3)</code> should return <code>"abcabcabc"</code>.
    testString: assert(repeatStringNumTimes("abc", 3) === "abcabcabc");
  - text: <code>repeatStringNumTimes("abc", 4)</code> should return <code>"abcabcabcabc"</code>.
    testString: assert(repeatStringNumTimes("abc", 4) === "abcabcabcabc");
  - text: <code>repeatStringNumTimes("abc", 1)</code> should return <code>"abc"</code>.
    testString: assert(repeatStringNumTimes("abc", 1) === "abc");
  - text: <code>repeatStringNumTimes("*", 8)</code> should return <code>"********"</code>.
    testString: assert(repeatStringNumTimes("*", 8) === "********");
  - text: <code>repeatStringNumTimes("abc", -2)</code> should return <code>""</code>.
    testString: assert(repeatStringNumTimes("abc", -2) === "");
  - text: The built-in <code>repeat()</code> method should not be used.
    testString: assert(!/\.repeat/g.test(code));
  - text:  <code>repeatStringNumTimes("abc", 0)</code> should return <code>""</code>.
    testString: assert(repeatStringNumTimes("abc", 0) === "");
    

Challenge Seed

function repeatStringNumTimes(str, num) {
  return str;
}

repeatStringNumTimes("abc", 3);

Solution

function repeatStringNumTimes(str, num) {
  if (num < 1) return '';
  return num === 1 ? str : str + repeatStringNumTimes(str, num-1);
}

repeatStringNumTimes("abc", 3);