repeat a string
n times
[ Array | join ]

Repeat a string n times

Write a function that returns a new string that consists of the original string repeated n times. The original string remains unchanged.


Example ...

Enter a string and a number ...

"All work and no play!" → string

3 → n number of times to repeat string

The function returns ...

"All work and no play!All work and no play!All work and no play!" → string repeated 3 times

The original string is repeated n times.

The original string is unchanged.

Strings are a sequence of zero or more characters written inside quotes used to represent text.

Strings may consist of letters, numbers, symbols, words, or sentences.

Strings are immutable, they cannot be changed.

Each character in a string has an index.

The first character will be index 0 the second character will be index 1 and so on.

There are two ways to access an individual character in a string.

charAt() method

const str1 = "abc"; string

str1.charAt(0); character at index 0 → "a"

str1.charAt(1); character at index 1 → "b"

str1.charAt(2); character at index 2 → "c"

str1.charAt(3); character at index 3 → "" index not found

Alternatively use at() or slice() methods

bracket notation []

const str2 = "abc"; string

str2[0]; character at index 0 → "a"

str2[1]; character at index 1 → "b"

str2[2]; character at index 2 → "c"

str2[3]; character at index 3 → undefined index not found


Arrays are used to store multiple values in a single variable.

Each value is called an element, and each element has a numeric position in the array, known as its index.

Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.

Arrays can contain any data type, including numbers, strings, and objects.

const arr1 = [2, 4, 6]; array

arr1[0]; element at index 0 → 2

arr1[1]; element at index 1 → 4

arr1[2]; element at index 2 → 6

arr1[3]; element at index 3 → undefined index not found


Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Repeat a string using ↴

Array constructor → creates an Array object.

join() method → returns an array as a string.


Array constructor

Arrays can be created using a constructor with a single number parameter.

An array is created with its length property set to that number, and the array elements are empty slots.

Array() can be called with or without new

Create an array with 5 empty slots.

const arr2 = Array(5); // const arr2 = new Array(5);

console.log(arr2); returns ↴

[empty × 5] [ , , , , , ]

Join the array elements with "Hello" as the separator.

const arr3 = Array(5);

const newStr = arr3.join("Hello"); returns ↴

console.log(newStr);

"HelloHelloHelloHello" → repeated 4 times


join() method joins all elements of an array into a single string with a specified separator between each element. The original array is unchanged.

("") separator → returns a string joined with no spaces between each character.

(" ") separator → returns string joined with a single space between each element.

const arr4 = ["H", "e", "l", "l", "o"]; array

arr4.join(""); returns ↴

"Hello" → string

const arr5 = ["Hello", "World"]; array

arr5.join(" "); returns ↴

"Hello World" → string


Initialize a variable to hold the string to repeat.

const string1 = "All work and no play!" → user input

Initialize a variable to hold the number of times to repeat the string.

const number1 = 3 → user input


Define a function repeatString() to repeat a string n times.

function repeatString(str, num) {}

The function takes a string str and a number num and returns a new string that consists of the original string repeated num times. The original string is unchanged.

Create an array with num empty slots and join them with str

return Array(num + 1).join(str)

Array(num + 1) creates an array with num + 1 empty slots (to account for the zero index).

This is necessary because the join method will concatenate the string str between each of these slots.

join(str) method concatenates the elements of the array, inserting str between each element.

Since the array is empty, it effectively results in the string being repeated num times.

If num is negative (less than -1), RangeError is thrown.


Call the function with ↴

repeatString(string1, number1);


Repeat a string 3 times.

const string1 = "All work and no play!";

const number1 = 3;

function repeatString(str, num) {

return Array(num + 1).join(str);

}

call function

repeatString(string1, number1); returns ↴

"All work and no play!All work and no play!All work and no play!"

Returns RangeError if num less than -1


Alternative to prevent RangeError if num is negative ↴

Repeat a string -3 times.

const string2 = "All work and no play!";

const number2 = -3; → negative num

function repeatString2(str, num) {

return num > 0 ? Array(num + 1).join(str) : "";

}

repeatString2(string2, number2); returns ↴

""

Repeat a string n times

Enter number of times to repeat