String repeat

Lee·2022년 7월 16일

Algorithm

목록 보기
49/92
post-thumbnail

❓ String repeat

Q. Write a function that accepts an integer n and a string s as parameters, and returns a string of s repeated exactly n times.

Examples (input -> output)
6, "I" -> "IIIIII"
5, "Hello" -> "HelloHelloHelloHelloHello"

✔ Solution

//#my solution
function repeatStr(n, s) {
  let result = s.repeat(n);
  return result;
}

//other solution
function repeatStr (n, s) {
var str="";
for(var i=0; i < n; i++)
  str+=s;
  return str;
}
profile
Lee

0개의 댓글