repeat() method

The repeat() method in Apps Script is used to repeat a string a certain number of times, as specified by an integer argument. It takes one argument: the integer value that specifies how many times to repeat the string.

Example 1: Repeating a Country Name Multiple Times
Suppose we want to repeat the name of a country 5 times, separated by a comma and a space. We can use the repeat() method like so:

var country = "Canada";
var repeatedCountry = country.repeat(5);
Logger.log(repeatedCountry);

In this example, we define a variable country to hold the name of the country. We then use the repeat() method on the string to repeat the country name 5 times, and assign the result to a new variable repeatedCountry. Finally, we log the value of repeatedCountry to the console using the Logger.log() method.

Example 2: Generating a String of City Names
Suppose we have an array of city names, and we want to generate a string of all the city names separated by a comma and a space. We can use the repeat() method along with the join() method like so:

var cities = ["New York", "Paris", "Tokyo", "Rio de Janeiro"];
var cityList = cities.join(", ").repeat(2);
Logger.log(cityList);

In this example, we define an array cities to hold the city names. We then use the join() method on the array to generate a string of all the city names separated by a comma and a space. We use the repeat() method on the resulting string to repeat the city list 2 times, and assign the result to a new variable cityList. Finally, we log the value of cityList to the console using the Logger.log() method.

By using the repeat() method in Apps Script, we can easily repeat strings a certain number of times, which can be useful for generating lists, formatting output, and other tasks.