

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Repeating string for specific number of times using JavaScript
Problem
We are required to write a JavaScript function that takes in a string and a number as two arguments.
Our function should repeat the input string the number of times specified by the second argument and return the new repeating string.
Example
Following is the code −
const str = 'this'; const num = 8; const repeatSpecificTimes = (str = '', num = 1) => { const arr = [str]; let i = 1; while(i < num){ arr.push(arr[0]); i++; }; return arr.join(''); }; console.log(repeatSpecificTimes(str, num));
Output
thisthisthisthisthisthisthisthis
- Related Questions & Answers
- Repeating each character number of times their one based index in a string using JavaScript
- Number of times a string appears in another JavaScript
- Repeating letter string - JavaScript
- Finding number that appears for odd times - JavaScript
- Repeating tuples N times in Python
- How to replicate a string for a specified number of times in MySQL?
- How to repeat a string for a specified number of times in Golang?
- Checking for overlapping times JavaScript
- Find first repeating character using JavaScript
- How to print the number of times a specific character repeats in a statement in javascript?
- Return index of first repeating character in a string - JavaScript
- Inserting empty string in place of repeating values in JavaScript
- C++ program to concatenate a string given number of times?
- Concatenate a string given number of times in C++ programming
- C/C++ Program for Finding the Number Occurring Odd Number of Times?
Advertisements