Algorithm Exercise 7: Repeating the Algorithm

This algorithm is relatively simple, we use a small example to demonstrate:

numConcatenates a specified string of times in a loop  num, and returns an empty string if it is a negative number.

function repeat(str, num) {

    if(num<0){
        return "";
    }else if(num==1){
        return str;
    }else{
       s="";
        for(i=1;i<=num;i++){
            s + = str;
        }
        return s;
    }
}

repeat("abc", 3);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325212878&siteId=291194637