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

1.7 KiB

id title challengeType forumTopicId dashedName
afcc8d540bea9ea2669306b6 Repetir uma string Repetir uma string 5 16041 repeat-a-string-repeat-a-string

--description--

Repita uma string passada str (primeiro argumento), num vezes (segundo argumento). Retorne uma string vazia se num não for um número positivo. Para o propósito do desafio, Não use o método integrado .repeat().

--hints--

repeatStringNumTimes("*", 3) deve retornar a string ***.

assert(repeatStringNumTimes('*', 3) === '***');

repeatStringNumTimes("abc", 3) deve retornar a string abcabcabc.

assert(repeatStringNumTimes('abc', 3) === 'abcabcabc');

repeatStringNumTimes("abc", 4) deve retornar a abcabcabcabc.

assert(repeatStringNumTimes('abc', 4) === 'abcabcabcabc');

repeatStringNumTimes("abc", 1) deve retornar a string abc.

assert(repeatStringNumTimes('abc', 1) === 'abc');

repeatStringNumTimes("*", 8) deve retornar a ********.

assert(repeatStringNumTimes('*', 8) === '********');

repeatStringNumTimes("abc", -2) deve retornar uma string vazia ("").

assert(repeatStringNumTimes('abc', -2) === '');

O método integrado repeat() não deve ser usado.

assert(!/\.repeat/g.test(code));

repeatStringNumTimes("abc", 0) deve retornar "".

assert(repeatStringNumTimes('abc', 0) === '');

--seed--

--seed-contents--

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

repeatStringNumTimes("abc", 3);

--solutions--

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

repeatStringNumTimes("abc", 3);