curriculum/challenges/english/blocks/lecture-working-with-string-modification-methods/67326c3392068ec6184a0c95.md
When working with JavaScript, you may encounter situations where you need to repeat a string a specific number of times.
Whether you're generating repeated patterns or simply duplicating text, the repeat() method provides a simple and effective way to achieve this.
The repeat() method is a built-in function in JavaScript that allows you to repeat a string a specified number of times. Here is the basic syntax:
string.repeat(count);
string is the string that you want to repeat, and count is the number of times you want the string to be repeated. Here's an example:
:::interactive_editor
let word = "Hello!";
let repeatedWord = word.repeat(3);
console.log(repeatedWord); // "Hello!Hello!Hello!"
:::
In this case, the string Hello! is repeated three times, resulting in Hello!Hello!Hello!.
While the repeat() method is useful, there are a few exceptions and limitations to keep in mind.
The count parameter must be a non-negative number. If you pass a negative number, JavaScript will throw a RangeError.
let word = "Test";
console.log(word.repeat(-1)); // Throws RangeError: Invalid count value
The count must be a finite number. If you try to repeat a string an infinite number of times or use Infinity as the count, you will also get a RangeError.
In JavaScript, Infinity is a special value that represents an infinite quantity. It's used to denote numbers that are larger than any finite number.
let word = "Test";
console.log(word.repeat(Infinity)); // Throws RangeError: Invalid count value
If the count is not an integer (such as a decimal like 2.5), the repeat() method will round it down to the nearest integer.
:::interactive_editor
let word = "Test";
console.log(word.repeat(2.5)); // "TestTest"
:::
If you pass 0 as the count, the repeat() method will return an empty string.
:::interactive_editor
let word = "Test";
console.log(word.repeat(0)); // ""
:::
The repeat() method can simplify tasks that involve string duplication, making your code more concise and readable.
Whether you're generating repeated text patterns or filling a space with characters, repeat() can save you from writing loops or more complex code.
You are not limited to passing a number directly into the repeat() method. You can also pass a variable that stores a number value.
:::interactive_editor
let count = 4;
let word = "Test";
let repeatedWord = word.repeat(count);
console.log(repeatedWord); // TestTestTestTest
:::
In this example, the count variable stores the number of repetitions. This can be useful when the number of repetitions depends on user input or other dynamic values in your program.
What is the result of calling "Hello".repeat(3); in JavaScript?
"HelloHelloHello"
"Hello Hello Hello"
Consider how the repeat() method concatenates the repeated string.
"Hello!"
Consider how the repeat() method concatenates the repeated string.
"HelloHello"
Consider how the repeat() method concatenates the repeated string.
1
What happens if you try to call repeat() with a negative number?
The string is repeated once.
Think about how repeat() handles invalid count values.
The string is repeated the absolute value of the negative number.
Think about how repeat() handles invalid count values.
A RangeError is thrown.
An empty string is returned.
Think about how repeat() handles invalid count values.
3
If you call "*".repeat(0), what is the output?
"*"
Consider what happens when you ask to repeat something zero times.
""
null
Consider what happens when you ask to repeat something zero times.
"*****"
Consider what happens when you ask to repeat something zero times.
2