akmalist / JavasSctipt-basic-algoritms

FreeCodeCamp tests
0 stars 0 forks source link

Basic Algorithm Scripting: Repeat a String Repeat a String #4

Open akmalist opened 6 years ago

akmalist commented 6 years ago
function repeatStringNumTimes(str, num) {
  // repeat after me
var array = [];
    for(var i = 0; i < num;)
        array[i++] = str;
    return array.join('');
}

repeatStringNumTimes("abc", 3);
akmalist commented 6 years ago
function repeatStringNumTimes(str, num) {
  var accumulatedStr = '';

  while (num > 0) {
    accumulatedStr += str;
    num--;
  }

  return accumulatedStr;
akmalist commented 6 years ago
function repeatStringNumTimes(str, num) {
  if(num < 0)
    return "";
  if(num === 1)
    return str;
  else
    return str + repeatStringNumTimes(str, num - 1);
}
akmalist commented 6 years ago
function repeatStringNumTimes(str, num) {
  return num > 0 ? str.repeat(num) : '';
}

repeatStringNumTimes("abc", 3);
akmalist commented 5 years ago

using express routing :


app.get("/repeat/:link/:num", function(req,res){

  function repeatMe(str , num){
    str = req.params.link;
    num = req.params.num;
  if (num<1){
    str =" ";
  }else{
    return (str+" ").repeat(num);
  }

  }
  // console.log(req.params.link);
  // console.log(req.params.num);
res.send(repeatMe(" link ", +" num "));

});